Make field read only after creation for that instance.

288 views
Skip to first unread message

Dipen bhatt

unread,
Jun 26, 2018, 8:16:21 AM6/26/18
to Django REST framework
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.


est...@eventcloud.co

unread,
Jun 29, 2018, 2:37:11 AM6/29/18
to Django REST framework
We have a very similar need, please see this discussion I recently created to see how I'm currently getting around this sort of issue
Reply all
Reply to author
Forward
0 new messages