Reputation: 1042
I am trying to understand the code flow in Django rest framework from view class to serializer class. Like we know if we create an object of a class, the class gets instantiate with that object. And if there are other functions inside that class, we can call that function using the object we just created followed by dot and that function name.
It looks something like this.
class Person:
def __init__(self, name, address):
self.name = name
self.address = address
def details(self):
print (f"My name is {self.name} and I live in {self.address}")
obj1 = Person("John","London")
obj1.details()
For example in the above code, when we run obj1= .... , the person class will instantiate with values name and address. And then we can call the details function using the class object that we just created obj1.
Now in Django Rest,
class LeadsView(APIView):
permission_classes = [IsAuthenticated]
def put(self, request, pk=None, *args, **kwargs):
id = pk
abc = Lead.objects.get(id=id)
print("before serializer")
serializer = LeadSerializer(abc,data=request.data)
if serializer.is_valid():
print("after serializer validation")
serializer.save()
print("after save")
return Response({
"message": "Coupon hase been updated",
"data": serializer.data
}, status=status.HTTP_200_OK)
return Response(serializer.data)
class Leadserializer(serializers.ModelSerializer):
class Meta:
model = Lead
fields = '__all__'
def update(self,instance,validated_data):
..........
print("iam inside the serializer class")
return instance
Here I am trying to make the django code analogus to the simple class object because thats how we should be learning, but no one seems to be teaching that way. Here I think the serializer class is instantiated in this line of code serializer = LeadSerializer(abc,data=request.data)
where the data is deserialized whereas abc is serialized.
However, just like the above simple class object case, I haven't called any update function of the serializer class using serializer object that I have created. So how the update function is called here??
Also, the print output are are as follows:
before serializer
after serializer validation
iam inside the serializer class
after save
Here seeing the output pattern, we came to know that only after save function, the update function seems to be running.
Upvotes: 1
Views: 1043
Reputation: 201
When you call serializer.save() on an instance of the model serializer you will end up running the code that updates the model instance, and runs the update function (self.update()).
This is a part of the code for the save() function inside the ModelSerializer:
validated_data = {**self.validated_data, **kwargs}
if self.instance is not None:
self.instance = self.update(self.instance, validated_data)
assert self.instance is not None, (
'`update()` did not return an object instance.'
)
else:
self.instance = self.create(validated_data)
assert self.instance is not None, (
'`create()` did not return an object instance.'
)
return self.instance
Upvotes: 1