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
Expanded and clarified documentation around Serializers in the Django REST Framework section. Added a detailed walkthrough for creating a serializer using the Artist model, and subsequent steps for url mapping and views.
Copy file name to clipboardExpand all lines: docs/images/index.md
+89-6Lines changed: 89 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -209,21 +209,104 @@ def index(_request):
209
209
```
210
210
211
211
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
-

212
+

213
213
214
214
Until here we just looked at Django stuff. Now we will dive into Django Rest Framework(DRF) stuff.
215
215
216
216
## Serializers
217
217
218
-
- 🎬 Lecture : The role and implementation of serializers.
219
-
- 💻 Exercise : Students practice converting data formats using serializers.
220
-
- 💡 Purpose: Grasping data format conversions essential in API communication.
218
+
From now on we will dive into DRF specific work.
219
+
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
+
221
+
222
+
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
+
224
+
So now we will use our Artist model to create our first endpoint.
225
+
226
+
You need to create a file called `serializers.py` with creating the serializer for the Artist Model.
1. Here we import the serializers module from rest_framework
239
+
2. We also import the model Artist
240
+
3. Create a `ArtistSerializer` inheriting from `serializers.HyperlinkedModelSerializers`. It will do a few things.
241
+
1. Create a Serializer based on the Artist Model based on the model field in the Meta class.
242
+
2. Create hyperlinks for the relationships
243
+
4. We need to pass explicitly the fields from the Artist model that will be in the serializer at the `fields` in the Meta class.
244
+
245
+
With the serializer in place we need more 2 steps, the url mapping and the view.
246
+
247
+
Let's do both in sequence, first the view. For the view we are going to use a [ModelViewSet](https://www.django-rest-framework.org/api-guide/viewsets/#modelviewset). Inside our file `music.views.py` we need to add this snipper.
248
+
249
+
```python
250
+
# music.views.py
251
+
...
252
+
classArtistViewSet(viewsets.ModelViewSet):
253
+
queryset = Artist.objects.all()
254
+
serializer_class = ArtistSerializer
255
+
```
256
+
1. Here we create a ViewSet class that will be responsible to create our CRUD(+ list) views. It inherits from `ModelViewSet`.
257
+
2.`queryset` parameter tells DRF what do list, this will be shared across all the views
258
+
3.`serializer_class` is self-explanatory
259
+
260
+
Don't forget to add the imports at the beggining of the file.
261
+
```python
262
+
from rest_framework import viewsets
263
+
264
+
from music.models import Artist
265
+
from music.serializers import ArtistSerializer
266
+
```
221
267
222
268
## Building an API - Part I
223
269
224
-
- 💻 Exercise: Students start building a basic API, integrating learned concepts.
225
-
- 💡 Purpose: Applying accumulated knowledge in a practical project.
270
+
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
+
This will be the code:
272
+
```python
273
+
# music.urls.py
274
+
from django.urls import path, include
275
+
from rest_framework import routers
276
+
277
+
from . import views
278
+
from .views import ArtistViewSet
279
+
280
+
router = routers.DefaultRouter()
281
+
router.register(r'artists', ArtistViewSet)
282
+
283
+
urlpatterns = [
284
+
path('', include(router.urls)),
285
+
# path('', views.index, name='index'),
286
+
]
287
+
```
288
+
1. Import the routers from DRF
289
+
2. Import the ArtistViewSet
290
+
3. Instantiate the DefaultRouter
291
+
4. Register the artists route to the ArtistViewSet
292
+
5. So we include it on our urlpatterns
293
+
6. And comment the previous endpoint we have, to avoid conflicts
294
+
295
+
296
+
Now to see it all working together we need to create the migrations for our models with the following steps.
297
+
```shell
298
+
cd first_api
299
+
./manage.py makemigrations music
300
+
./manage.py migrate music
301
+
cd ..
302
+
task r
303
+
```
304
+
305
+
Now access http://127.0.0.1:8000/ to see your API working. 🥳
306
+
307
+
Congratulations now you have your first api working.
0 commit comments