Ask Your Question
2

Despite having "blank=True" in the field, why does Django EmbeddedModelField display "This field may not be blank" when executing a PUT request?

asked 2023-06-25 20:09:35 +0000

huitzilopochtli gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-06-25 20:34:01 +0000

david gravatar image

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', ...)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-25 20:09:35 +0000

Seen: 13 times

Last updated: Jun 25 '23