[Fixed]-Post contact for specific rent in rest framework

1👍

Since you don’t want rental passed as submitted data, you might want to remove it from your serializer.

class ContactSerializer(serializers.ModelSerializer):
    class Meta:
        model = Contact
        fields = ['buyer', 'email_id']  # omit `rental` field

The partial argument to the serializer is for PATCH requests, when you only want to validate the submitted fields, and update only those fields. (I feel that you’re passing partial=True as a workaround because your serializer doesn’t validate if you don’t pass rental.)

Once you’ve removed rental from your serializer, you should now be able to do:

serialized_data = self.serializer_class(data=request.data)

which should validate, given you passed a valid buyer and email_id values. Then, when you call save, you pass the rental instance

serialized_data.save(rental=rent)

Leave a comment