Discussion:
Make field read only after creation for that instance.
Dipen bhatt
2018-06-26 12:16:21 UTC
Permalink
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.
--
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.
e***@eventcloud.co
2018-06-29 06:37:10 UTC
Permalink
We have a very similar need, please see this discussion
<https://groups.google.com/forum/#!topic/django-rest-framework/ej4WqQFuTQ0>
I recently created to see how I'm currently getting around this sort of
issue
Post by Dipen bhatt
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.
...
created_by = model.ForeignKey(User)
....
# 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.
Loading...