Dipen bhatt
2018-06-26 12:16:21 UTC
I have multiple models that have a field created by(Foreign key to user) in
them. After making serializers for the models, i wanted that the created
field be required at the time of object creation but once the object is
created the field becomes read only.
Is there anyway to do this. For the sake of context below is an example
code.
class Test(model.Model):
...
created_by = model.ForeignKey(User)
....
class TestSerializer(serializer.Serializer):
# other fields
created_by = serializer.SlugRelatedField(queryset = User.objects.all(),
slug_field="username")
And after the Test object is created, any further put request will skip the
required = True(Like it is done for partial_update by making partial =
true) validation for that particular instance.
them. After making serializers for the models, i wanted that the created
field be required at the time of object creation but once the object is
created the field becomes read only.
Is there anyway to do this. For the sake of context below is an example
code.
class Test(model.Model):
...
created_by = model.ForeignKey(User)
....
class TestSerializer(serializer.Serializer):
# other fields
created_by = serializer.SlugRelatedField(queryset = User.objects.all(),
slug_field="username")
And after the Test object is created, any further put request will skip the
required = True(Like it is done for partial_update by making partial =
true) validation for that particular instance.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.