Django Rest Framework API #24 / PARTIAL UPDATE | PATCH Action | Method for ModelViewSet and ApiView

preview_player
Показать описание
What you will learn:
- How to override partial update and patch action | method inside Django REST Framework. -
- How to override partial update action | method inside ModelViewSet.
- How to override PATCH action | method inside ApiView.
-How to update specific fields of an object by sending the target fields only.
-Handling the KeyError of the update method.
-How to read the data that was sent through a PATCH request using (request) argument then update an object and save it in the database.

Music Channel : @tuningthecode

Source code:

Override UPDATE | PUT Action | Method:

APIView GET & POST Request and Response:

Override Create Action | CREATE Method:

Override Delete Action (destroy method) DELETE Request:

ModelViewset With Serializer and Router:
Рекомендации по теме
Комментарии
Автор

Excellent explanation. It helped me a lot with a project I'm currently working on.

humbertocruz
Автор

your videos are very detailed and explain everything really well! Thank you

nimeshyogarajan
Автор

I was looking for that for a couple of days (maybe more). Fortunately I found you! Thanks bro!

marcelobarba
Автор

Thanks, just in time for my project since tomorrow is my deadline.

marissavictorio
Автор

i didn't figure it out how you get the object in patch without id....!!!
and how your url works when you have a <int> and for list view request you don't have <int>...for me it raises an error..!!!

mohammadhoseyni
Автор

How were you able to accept the ID param in the URL? (i.e. I have to type `localhost:8000/api/?id=1`) whereas you only type `/1` to return the item with ID=1. I have looked all through your git repo but cannot find how you specified this behavior

MrAustin
Автор

Instead of a try: except: if "plan_name" in ...: is always better and faster.

matthewsheeran
Автор

long time doubt i have, what does serializer in django

NandWebDev
Автор

Thanks, but I am wondering it we have to specify all the attributes. With APIView you can do something like this: (with as main object, as example: Project)
```
def put(self, request, id=None):
try:
project = Project.objects.get(id=id)
except Exception:
return Response({"error": "object not found in the database"},
serializer = ProjectSerializer(project, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response({"status": "success", "data": serializer.data}, status=status.HTTP_200_OK)
else:
return Response({"status": "error", "data": serializer.errors},
```
it is a put method but specifying partial=true, it gives indeed the same result as a patch. We can just update one field. And as you can see we don't have to specify all the attributes of the object. So I am astonished that with the ModelViewSet we have to. It is clear but it is not DRY.

chachacha
Автор

how to delete this car_plan relation object?

devopsliveuz
Автор

Some of the videos are in private.. I can't to watch those videos...why ❓

NandWebDev