[Django]-Django Rest Framework set a field read_only after record is created

2đź‘Ť

Use __init__ method of serializer to make it read when object is being updated:

class AmountGivenSerializer(serializers.ModelSerializer):  

    def __init__(self, *args, **kwargs):
        """If object is being updated don't allow contact to be changed."""
        super().__init__(*args, **kwargs)
        if self.instance is not None:
            self.fields.get('parent').read_only = True
            # self.fields.pop('parent') # or remove the field


    mode_of_payment = serializers.PrimaryKeyRelatedField(queryset=ModeOfPayment.objects.all())
    contact_detail = ContactSerializer(source='contact', read_only=True)
    contact = serializers.PrimaryKeyRelatedField(queryset=Contact.objects.all())

    class Meta:
        model = AmountGiven
        depth = 1
        fields = (
            'id', 'contact', 'contact_detail', 'amount', 'given_date', 'created'
        )

Using self.context['view'].action is not recommended as it will not work when using the serializer out of DRF, eg. in normal Django views. It’s best to use self.instance as it will work in every situation.

2đź‘Ť

If your viewset is a ModelViewSet, you can overwrite the perform_update hook (because ModelViewSet inherits from GenericAPIView (take a look at “Save and deletion hooks”). You can access the old contact using the serializer’s instance field:

class MyViewSet(viewsets.ModelViewSet):
    # ... other stuff

    def perform_update(self, serializer):
        serializer.save(contact=serializer.instance.contact)

So you will have to provide a contact, but no matter which contact you provide, it will always use the old saved contact when updating.

👤slider

Leave a comment