@@ -337,6 +337,133 @@ urlpatterns = [
337
337
```
338
338
1 . We added a new import for the AlbumViewSet
339
339
2 . We added the routes for albums
340
+
341
+ Next is the ` AlbumViewSet ` in the ` music.views.py ` . I will break it down in some steps.
342
+
343
+ ``` python
344
+ class AlbumViewSet (viewsets .ViewSet ):
345
+ queryset = Album.objects.all()
346
+ serializer_class = AlbumSerializer
347
+ ```
348
+ 1 . Here we create a class ` AlbumViewSet ` inheriting from ` views.ViewSet ` , pay attention, this is nos a model view set.
349
+ 2 . Set the ` queryset `
350
+ 3 . Set ` serializer_class ` , we are going to talk about this ` AlbumSerializer ` later
351
+
352
+ After this still in the same view we are going through what DRF call actions.
353
+ Instead of have methods in the view set for get, post, delete... It has functions based on actions. The actions are the ones below:
354
+
355
+ ``` python
356
+ class AlbumViewSet (viewsets .ViewSet ):
357
+ queryset = Album.objects.all()
358
+ serializer_class = AlbumSerializer
359
+
360
+ def list (self , request ):
361
+ """
362
+ List the resources, albums in this case
363
+ """
364
+ pass
365
+
366
+ def create (self , request ):
367
+ """
368
+ List the resources, albums in this case
369
+ """
370
+ pass
371
+
372
+ def retrieve (self , request , pk = None ):
373
+ """
374
+ Retrieve a single resources, album in this case
375
+ """
376
+ pass
377
+
378
+ def update (self , request , pk = None ):
379
+ """
380
+ Update the resource, album in this case
381
+ """
382
+ pass
383
+
384
+ def partial_update (self , request , pk = None ):
385
+ """
386
+ Partially update the resource, album in this case
387
+ """
388
+ pass
389
+
390
+ def destroy (self , request , pk = None ):
391
+ """
392
+ Delete the resource, album in this case
393
+ """
394
+ pass
395
+ ```
396
+
397
+ With the actions in place we will fill each of these methods.
398
+ First the ` list ` method.
399
+
400
+ ``` python
401
+ def list (self , request ):
402
+ serializer = self .serializer_class(self .queryset, many = True )
403
+ return Response(serializer.data)
404
+ ```
405
+ 1 . Here we just need to serialize the queryset and return it as a
406
+ 2 . Don't forget to import the ` from rest_framework.response import Response `
407
+
408
+ The following action will be ` create `
409
+
410
+ ``` python
411
+ def create (self , request ):
412
+ serializer = self .serializer_class(data = request.data)
413
+ serializer.is_valid(raise_exception = True )
414
+ serializer.save()
415
+ return Response(serializer.data, status = status.HTTP_201_CREATED )
416
+ ```
417
+ 1 . Here we need to serialize the ` request.data `
418
+ 2 . Check if it ` is_valid `
419
+ 3 . Save our Album(There is caveat here I will show later how to fix)
420
+ 4 . And return a response with the ` serializer.data ` a and a https status
421
+ 5 . Don't forget to import ` from rest_framework import status `
422
+
423
+ Next is ` retrieve `
424
+ ``` python
425
+ def retrieve (self , request , pk = None ):
426
+ album = get_object_or_404(Album, pk = pk)
427
+ serializer = self .serializer_class(album)
428
+ return Response(serializer.data)
429
+ ```
430
+ 1 . We try to get the album using the ` get_object_or_404 ` from ` from rest_framework.generics import get_object_or_404 `
431
+ 2 . serialize it and send it back as a response
432
+
433
+ So we have the ` update `
434
+ ``` python
435
+ def update (self , request , pk = None ):
436
+ album = get_object_or_404(Album, pk = pk)
437
+ serializer = self .serializer_class(album, data = request.data)
438
+ serializer.is_valid(raise_exception = True )
439
+ serializer.save()
440
+ return Response(serializer.data)
441
+ ```
442
+ 1 . Similarly to the previous one but this time we update just the needed fields
443
+
444
+ For ` partial_update ` is close to the one above
445
+ ``` python
446
+ def partial_update (self , request , pk = None ):
447
+ album = get_object_or_404(Album, pk = pk)
448
+ serializer = self .serializer_class(album, data = request.data, partial = True )
449
+ serializer.is_valid(raise_exception = True )
450
+ serializer.save()
451
+ return Response(serializer.data)
452
+ ```
453
+ 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.
454
+
455
+ Last but not least ` destroy ` action to delete albums
456
+ ``` python
457
+ def destroy (self , request , pk = None ):
458
+ album = get_object_or_404(Album, pk = pk)
459
+ album.delete()
460
+ return Response(status = status.HTTP_204_NO_CONTENT )
461
+ ```
462
+ 1 . Here we need to get the album and call the ` .delete() ` on it
463
+
464
+
465
+ With ` destroy ` set the we finish the change on ` music.views.py `
466
+
340
467
## Bonus content
341
468
342
469
### Serializers deep dive
0 commit comments