Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The "blank=True" attribute in Django only applies to form validation and has no effect on API requests. When executing a PUT request, Django REST Framework will check for the presence of the field in the request data. If it is not present, it will raise a "This field may not be blank" error even if "blank=True" is set on the model field.

To allow the field to be optional in API requests, you can add "required=False" to the serializer field for that model field. This will tell Django REST Framework to not raise an error if the field is missing in the request data. For example:

class MyEmbeddedModelSerializer(serializers.ModelSerializer):
    my_field = EmbeddedModelField(MyEmbeddedModel, required=False)

    class Meta:
        model = MyModel
        fields = ('id', 'my_field', ...)