I don't understand how Django-Rest-Framework serializers work -
this may seem dumb question feel django-rest-framework not explain how works. there black box magic confuses configuration instructions (in opinion).
for instance, says can override create or update within serializer. when view post, can send data serializer has update method declared.
class myserializer(serializers.modelserializer): class meta: model = mymodel def update(self, instance, validated_data):
does update called when model exists , we're updating of it's data? or call create when creates new one?
if add method class,
def create(self, validated_data): return myobject.objects.create(**validated_data)
is method must called in order add new object? , ability override should put in serializer, if not declared default method parameters that's being called?
there black box magic confuses configuration instructions (in opinion).
if there in documentation think can improved, feel free to submit pull request update. if seems reasonable, merged , show in future release.
or call create when creates new one?
create
called when serializer not initialized model instance, data passed it. once serializer.save()
called, serializer check if instance passed in , directly calls create
model instance created , saved in database.
does update called when model exists , we're updating of it's data?
update
called when serializer initialized model instance (or other object) , serializer.save()
called. equivalent model.objects.update()
when compared model.objects.create()
.
is method must called in order add new object?
serializers designed saved (including object creation , updating) using central serializer.save()
method. similar how model can saved or created using model.save()
method.
and ability override should put in serializer, if not declared default method parameters that's being called?
it recommended override create
, update
on serializer level if need change logic how model , related objects need saved, such when working nested serializers.
Comments
Post a Comment