Skip to content

Commit dffd041

Browse files
committed
Fix typos and add missing newlines in documentation
Corrected several typos and improved formatting by adding missing newlines for better readability. This ensures the documentation is clearer and more professional.
1 parent 426e624 commit dffd041

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

docs/index.md

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ INSTALLED_APPS = [
118118
]
119119
...
120120
```
121+
121122
## Django Models
122123

123124
Now the next step is create the models we are going to use in our API to represent the domain models.
@@ -140,10 +141,13 @@ class Artist(models.Model):
140141
```
141142

142143
Don't forget to import the models
144+
143145
```python
144146
from django.db import models
145147
```
148+
146149
Now the album model
150+
147151
```python
148152
# music/models.py
149153
class Album(models.Model):
@@ -171,7 +175,6 @@ class Song(models.Model):
171175

172176
Now let's go to the URL Mapping, we need to associate the url with the handler functions that are called as view in Django. To create a simple endpoint that works.
173177

174-
175178
```python
176179
# first_api/urls.py
177180
from django.contrib import admin
@@ -211,19 +214,19 @@ def index(_request):
211214
So no you can use the command `task r` to start our django server, so you can access http://127.0.0.1:8000/ to see it.
212215
![my_first_api.png](images/my_first_api.png)
213216

214-
Until here we just looked at Django stuff. Now we will dive into Django Rest Framework(DRF) stuff.
217+
Until here we just looked at Django stuff. Now we will dive into Django Rest Framework(DRF) stuff.
215218

216219
## Serializers
217220

218221
From now on we will dive into DRF specific work.
219222
The concept I want to present you is the Serializer. That is responsible for parse the data received(usually through a HTTP request, since we are creating an API) into python native types, and sometime into our Django models.
220223

221-
222224
Serializers are deeply inspired into [Django Forms](https://docs.djangoproject.com/en/5.0/topics/forms/#forms-in-django) and [Django Model Forms](https://docs.djangoproject.com/en/5.0/topics/forms/modelforms/)
223225

224226
So now we will use our Artist model to create our first endpoint.
225227

226228
You need to create a file called `serializers.py` with creating the serializer for the Artist Model.
229+
227230
```python
228231
from rest_framework import serializers
229232

@@ -235,6 +238,7 @@ class ArtistSerializer(serializers.HyperlinkedModelSerializer):
235238
model = Artist
236239
fields = ['name']
237240
```
241+
238242
1. Here we import the serializers module from rest_framework
239243
2. We also import the model Artist
240244
3. Create a `ArtistSerializer` inheriting from `serializers.HyperlinkedModelSerializers`. It will do a few things.
@@ -253,11 +257,13 @@ class ArtistViewSet(viewsets.ModelViewSet):
253257
queryset = Artist.objects.all()
254258
serializer_class = ArtistSerializer
255259
```
260+
256261
1. Here we create a ViewSet class that will be responsible to create our CRUD(+ list) views. It inherits from `ModelViewSet`.
257262
2. `queryset` parameter tells DRF what do list, this will be shared across all the views
258263
3. `serializer_class` is self-explanatory
259264

260265
Don't forget to add the imports at the beggining of the file.
266+
261267
```python
262268
from rest_framework import viewsets
263269

@@ -269,6 +275,7 @@ from music.serializers import ArtistSerializer
269275

270276
Ok, now we just need to map our ArtistViewSet to a URL. In our `music.urls.py` we are going to use one more resource that DRF provides us, the [Default Router](https://www.django-rest-framework.org/api-guide/routers/#defaultrouter). It will create a set of common routes for our ViewSet.
271277
This will be the code:
278+
272279
```python
273280
# music/urls.py
274281
from django.urls import path, include
@@ -285,15 +292,16 @@ urlpatterns = [
285292
# path('', views.index, name='index'),
286293
]
287294
```
295+
288296
1. Import the routers from DRF
289297
2. Import the ArtistViewSet
290298
3. Instantiate the DefaultRouter
291299
4. Register the artists route to the ArtistViewSet
292300
5. So we include it on our urlpatterns
293301
6. And comment the previous endpoint we have, to avoid conflicts
294302

295-
296303
Now to see it all working together we need to create the migrations for our models with the following steps.
304+
297305
```shell
298306
cd first_api
299307
./manage.py makemigrations music
@@ -314,10 +322,10 @@ Congratulations now you have your first api working.
314322

315323
## Building an API - Part II
316324

317-
318325
Now that you've explored some of the shortcuts provided by DRF, let's delve into creating an endpoint for the album model using a plain Serializer, without relying heavily on shortcuts.
319326

320327
Let's start by the urls part. We gonna need to add the new route to our `music.urls.py`. Now it should look like this.
328+
321329
```python
322330
from django.urls import path, include
323331
from rest_framework import routers
@@ -335,6 +343,7 @@ urlpatterns = [
335343
]
336344

337345
```
346+
338347
1. We added a new import for the AlbumViewSet
339348
2. We added the routes for albums
340349

@@ -345,6 +354,7 @@ class AlbumViewSet(viewsets.ViewSet):
345354
queryset = Album.objects.all()
346355
serializer_class = AlbumSerializer
347356
```
357+
348358
1. Here we create a class `AlbumViewSet` inheriting from `views.ViewSet`, pay attention, this is nos a model view set.
349359
2. Set the `queryset`
350360
3. Set `serializer_class`, we are going to talk about this `AlbumSerializer` later
@@ -402,7 +412,8 @@ First the `list` method.
402412
serializer = self.serializer_class(self.queryset, many=True)
403413
return Response(serializer.data)
404414
```
405-
1. Here we just need to serialize the queryset and return it as a
415+
416+
1. Here we just need to serialize the queryset and return it as a
406417
2. Don't forget to import the `from rest_framework.response import Response`
407418

408419
The following action will be `create`
@@ -414,23 +425,27 @@ The following action will be `create`
414425
serializer.save()
415426
return Response(serializer.data, status=status.HTTP_201_CREATED)
416427
```
428+
417429
1. Here we need to serialize the `request.data`
418430
2. Check if it `is_valid`
419431
3. Save our Album(There is caveat here I will show later how to fix)
420432
4. And return a response with the `serializer.data`a and a https status
421433
5. Don't forget to import `from rest_framework import status`
422434

423435
Next is `retrieve`
436+
424437
```python
425438
def retrieve(self, request, pk=None):
426439
album = get_object_or_404(Album, pk=pk)
427440
serializer = self.serializer_class(album)
428441
return Response(serializer.data)
429442
```
443+
430444
1. We try to get the album using the `get_object_or_404` from `from rest_framework.generics import get_object_or_404`
431445
2. serialize it and send it back as a response
432446

433447
So we have the `update`
448+
434449
```python
435450
def update(self, request, pk=None):
436451
album = get_object_or_404(Album, pk=pk)
@@ -439,9 +454,11 @@ So we have the `update`
439454
serializer.save()
440455
return Response(serializer.data)
441456
```
457+
442458
1. Similarly to the previous one but this time we update just the needed fields
443459

444460
For `partial_update` is close to the one above
461+
445462
```python
446463
def partial_update(self, request, pk=None):
447464
album = get_object_or_404(Album, pk=pk)
@@ -450,17 +467,19 @@ For `partial_update` is close to the one above
450467
serializer.save()
451468
return Response(serializer.data)
452469
```
470+
453471
1. the only difference here from the `update` above is the `partial=True` so it know that it just need to update some fields not all of them.
454472

455473
Last but not least `destroy` action to delete albums
474+
456475
```python
457476
def destroy(self, request, pk=None):
458477
album = get_object_or_404(Album, pk=pk)
459478
album.delete()
460479
return Response(status=status.HTTP_204_NO_CONTENT)
461480
```
462-
1. Here we need to get the album and call the `.delete()` on it
463481

482+
1. Here we need to get the album and call the `.delete()` on it
464483

465484
With `destroy` set the we finish the change on `music.views.py`
466485

@@ -490,10 +509,12 @@ The first is `create`
490509
artist, created = Artist.objects.get_or_create(name=artist_data['name'])
491510
return Album.objects.create(artist=artist, **validated_data)
492511
```
512+
493513
1. Where we create the album but since the album has artist as a nested model we need to create it here before try to save the album itself.
494514
2. Don't forget to import the `Album` model here with `from music.models import Album`
495515

496516
and the `update` method
517+
497518
```python
498519
def update(self, album, validated_data):
499520
artist_data = validated_data.pop('artist')
@@ -506,6 +527,7 @@ and the `update` method
506527

507528
return album
508529
```
530+
509531
1. To update we need to do the same with the `Artist` model, creating/getting it before save the Album
510532
2. So we get the new fields and save it
511533
3. Last we return the updated album
@@ -519,8 +541,11 @@ Now we have 2 resources here, artists and albums
519541
I hope that at this time you understand the amount of shortcuts DRF gives you at the same time, if you want to customize it, it's still possible.
520542

521543
## Building an API - Part III
544+
522545
### Easy version
546+
523547
We are going to start from the urls one more time. We will add the route for the songs in our `music.urls` like in the snippet below
548+
524549
```python
525550
from django.urls import path, include
526551
from rest_framework import routers
@@ -531,14 +556,15 @@ router = routers.DefaultRouter()
531556
router.register(r'artists', ArtistViewSet)
532557
router.register(r'albums', AlbumViewSet)
533558
# Add this new line below
534-
router.register(r'songs', SongViewSet)
559+
router.register(r'songs', SongViewSet)
535560

536561
urlpatterns = [
537562
path('', include(router.urls)),
538563
# path('', views.index, name='index'),
539564
]
540565
```
541-
After this we are going to create the `SongViewSet` in our `music.views` file using a `ModelViewSet` like in the snippet below. Also update Don't forget to add the import for the `SongSerializer` in your imports
566+
567+
After this we are going to create the `SongViewSet` in our `music.views` file using a `ModelViewSet` like in the snippet below. Also update Don't forget to add the import for the `SongSerializer` in your imports
542568

543569
```python
544570
from music.serializers import ArtistSerializer, AlbumSerializer, SongSerializer
@@ -563,26 +589,27 @@ With this part done you will be able to run you application and see something li
563589

564590
![final-version.png](images/final-version.png)
565591

566-
Now you api is complete! Congratulations! 🍾🎉🎊
592+
Now you api is complete! Congratulations! 🍾🎉🎊
567593

568594
## Bonus content
569595

570596
### Serializers deep dive
571597

572598
#### Types
599+
573600
1. [Serializer](https://www.django-rest-framework.org/api-guide/serializers/#serializers)
574601
2. [Model Serializer](https://www.django-rest-framework.org/api-guide/serializers/#modelserializer)
575602
3. [Hyperlinked Model Serializer](https://www.django-rest-framework.org/api-guide/serializers/#hyperlinkedmodelserializer)
576603

577604
### Serializer fields
605+
578606
1. [Boolean](https://www.django-rest-framework.org/api-guide/fields/#boolean-fields)
579607
2. [String](https://www.django-rest-framework.org/api-guide/fields/#string-fields)
580608
3. [Numeric](https://www.django-rest-framework.org/api-guide/fields/#numeric-fields)
581609
4. [Date and time](https://www.django-rest-framework.org/api-guide/fields/#date-and-time-fields)
582610

583611
### [Serializer relations](https://www.django-rest-framework.org/api-guide/relations/)
584612

585-
586613
### [Validators](https://www.django-rest-framework.org/api-guide/validators/)
587614

588615
### [Routers](https://www.django-rest-framework.org/api-guide/routers/)

0 commit comments

Comments
 (0)