Skip to content

Commit cac0c59

Browse files
committed
Update Django REST Framework documentation
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.
1 parent ef8b0f2 commit cac0c59

File tree

1 file changed

+89
-6
lines changed

1 file changed

+89
-6
lines changed

docs/index.md renamed to docs/images/index.md

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,21 +209,104 @@ def index(_request):
209209
```
210210

211211
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-
![my_first_api.png](images/my_first_api.png)
212+
![my_first_api.png](my_first_api.png)
213213

214214
Until here we just looked at Django stuff. Now we will dive into Django Rest Framework(DRF) stuff.
215215

216216
## Serializers
217217

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.
227+
```python
228+
from rest_framework import serializers
229+
230+
from music.models import Artist
231+
232+
233+
class ArtistSerializer(serializers.HyperlinkedModelSerializer):
234+
class Meta:
235+
model = Artist
236+
fields = ['name']
237+
```
238+
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+
class ArtistViewSet(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+
```
221267

222268
## Building an API - Part I
223269

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.
226308

309+
![first_api_working.png](../first_api_working.png)
227310
## Lunch/Break
228311

229312
- ⏳ Time for rest and informal discussions.

0 commit comments

Comments
 (0)