You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: docs/index.md
+38-11Lines changed: 38 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -118,6 +118,7 @@ INSTALLED_APPS = [
118
118
]
119
119
...
120
120
```
121
+
121
122
## Django Models
122
123
123
124
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):
140
141
```
141
142
142
143
Don't forget to import the models
144
+
143
145
```python
144
146
from django.db import models
145
147
```
148
+
146
149
Now the album model
150
+
147
151
```python
148
152
# music/models.py
149
153
classAlbum(models.Model):
@@ -171,7 +175,6 @@ class Song(models.Model):
171
175
172
176
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.
173
177
174
-
175
178
```python
176
179
# first_api/urls.py
177
180
from django.contrib import admin
@@ -211,19 +214,19 @@ def index(_request):
211
214
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.
212
215

213
216
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.
215
218
216
219
## Serializers
217
220
218
221
From now on we will dive into DRF specific work.
219
222
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.
220
223
221
-
222
224
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/)
223
225
224
226
So now we will use our Artist model to create our first endpoint.
225
227
226
228
You need to create a file called `serializers.py` with creating the serializer for the Artist Model.
229
+
227
230
```python
228
231
from rest_framework import serializers
229
232
@@ -235,6 +238,7 @@ class ArtistSerializer(serializers.HyperlinkedModelSerializer):
235
238
model = Artist
236
239
fields = ['name']
237
240
```
241
+
238
242
1. Here we import the serializers module from rest_framework
239
243
2. We also import the model Artist
240
244
3. Create a `ArtistSerializer` inheriting from `serializers.HyperlinkedModelSerializers`. It will do a few things.
@@ -253,11 +257,13 @@ class ArtistViewSet(viewsets.ModelViewSet):
253
257
queryset = Artist.objects.all()
254
258
serializer_class = ArtistSerializer
255
259
```
260
+
256
261
1. Here we create a ViewSet class that will be responsible to create our CRUD(+ list) views. It inherits from `ModelViewSet`.
257
262
2.`queryset` parameter tells DRF what do list, this will be shared across all the views
258
263
3.`serializer_class` is self-explanatory
259
264
260
265
Don't forget to add the imports at the beggining of the file.
266
+
261
267
```python
262
268
from rest_framework import viewsets
263
269
@@ -269,6 +275,7 @@ from music.serializers import ArtistSerializer
269
275
270
276
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.
271
277
This will be the code:
278
+
272
279
```python
273
280
# music/urls.py
274
281
from django.urls import path, include
@@ -285,15 +292,16 @@ urlpatterns = [
285
292
# path('', views.index, name='index'),
286
293
]
287
294
```
295
+
288
296
1. Import the routers from DRF
289
297
2. Import the ArtistViewSet
290
298
3. Instantiate the DefaultRouter
291
299
4. Register the artists route to the ArtistViewSet
292
300
5. So we include it on our urlpatterns
293
301
6. And comment the previous endpoint we have, to avoid conflicts
294
302
295
-
296
303
Now to see it all working together we need to create the migrations for our models with the following steps.
304
+
297
305
```shell
298
306
cd first_api
299
307
./manage.py makemigrations music
@@ -314,10 +322,10 @@ Congratulations now you have your first api working.
314
322
315
323
## Building an API - Part II
316
324
317
-
318
325
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.
319
326
320
327
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
+
321
329
```python
322
330
from django.urls import path, include
323
331
from rest_framework import routers
@@ -335,6 +343,7 @@ urlpatterns = [
335
343
]
336
344
337
345
```
346
+
338
347
1. We added a new import for the AlbumViewSet
339
348
2. We added the routes for albums
340
349
@@ -345,6 +354,7 @@ class AlbumViewSet(viewsets.ViewSet):
345
354
queryset = Album.objects.all()
346
355
serializer_class = AlbumSerializer
347
356
```
357
+
348
358
1. Here we create a class `AlbumViewSet` inheriting from `views.ViewSet`, pay attention, this is nos a model view set.
349
359
2. Set the `queryset`
350
360
3. Set `serializer_class`, we are going to talk about this `AlbumSerializer` later
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
542
568
543
569
```python
544
570
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
0 commit comments