Tag Archives: django DRF This field may not be null

[Solved] django DRF This field may not be null, This field cannot be blank

target

You need to change the original mandatory field to non mandatory

realization

First, update the fields in models, and add null = true, default = none, blank = true

1
2
name = CharField(max_length=64, verbose_name='name', null=True, default=None, blank=True)
account = CharField(max_length=64, verbose_name='account', null=True, default=None, blank=True)

serializers update

1
2
name = CharField(required=False)
account = CharField(required=False)

execute python manage.py makemigrations
execute python manage.py migrate

Error:

May appear this error:

1
This field may not be null.

or

1
This field cannot be blank.

Solution:

You need to change serializers to this

1
2
name = CharField(required=False, allow_blank=True, allow_null=True)
account = CharField(required=False, allow_blank=True, allow_null=True)

allow_blank=True to solve This field cannot be blank.
allow_null=True to solve This field may not be null.