Django ModelForm won't allow Null value for required field?

Chad

I have a Model with this field...

fulfillment_flag = models.CharField(pgettext_lazy('Delivery group field', 'Fulfillment Flag'), max_length=255,
                                        null=True, choices=FULFILLMENT_FLAG_CHOICES)

And here is my form...

class FulfillmentFlagForm(forms.ModelForm):

    class Meta:
        model = DeliveryGroup
        fields = ['fulfillment_flag', ]

    def clean_fulfillment_flag(self):
        return self.cleaned_data['fulfillment_flag'] or None

I have an HTML select drop down that has a blank value option at the top. Every time I select the blank option and click Save, form will not save it as a Null value on my model. It'll save any of the other fields though, just not the blank one. And it will tell me that the field is required as well.

How can I tell the form to just save the blank value as Null?

Chris Schäpers

https://docs.djangoproject.com/en/1.8/ref/models/fields/#null

Avoid using null on string-based fields such as CharField and TextField because empty string values will always be stored as empty strings, not as NULL. If a string-based field has null=True, that means it has two possible values for “no data”: NULL, and the empty string. In most cases, it’s redundant to have two possible values for “no data;” the Django convention is to use the empty string, not NULL.

For both string-based and non-string-based fields, you will also need to set blank=True if you wish to permit empty values in forms, as the null parameter only affects database storage (see blank).

Do blank=True instead and save "" instead of None.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

"This field cannot be null" error in a django 1.5 ModelForm

From Dev

Django - accessing modelform field value in template

From Dev

Set value of field for Django ModelForm in CreateView

From Dev

Django - Assign default value to field in ModelForm

From Dev

Set value of field for Django ModelForm in CreateView

From Dev

Django - UpdateView ModelForm setting initial value on queryset field

From Dev

Grails number field won't allow blank answer

From Dev

Firebase won't bind boolean value to field

From Dev

Need a non-editable field in POST data, but ModelForm won't let me

From Dev

How to hide a field in django modelform?

From Dev

Django ModelForm not showing field errors

From Dev

Save custom field in Django ModelForm

From Dev

django modelform property hidden field

From Java

JSONField doesn't allow null in Django 3.1

From Dev

Allow no whitespace in required form field

From Dev

Django | Decimal Field is required even tho i set it to NULL=True

From Dev

Django ForeignKey field required despite blank=True and null=True

From Dev

Django: Make certain fields in a ModelForm required=False

From Dev

Django creation ModelForm with OneToOneField hidden but required

From Dev

Django modelform formset first form required

From Dev

Django creation ModelForm with OneToOneField hidden but required

From Dev

ModelForm won't create instance of model

From Dev

Is it ok to use assert on a field I know won't be null?

From Dev

Django model field, alternative value return if null?

From Dev

Django admin inline calculated field won't save

From Dev

django field is required validators

From Dev

AngularJS Expression Won't Evaluate in Value Field of Input

From Dev

Javascript radio button value won't parse to PHP hidden field

From Dev

Can't update django datetime field to Null

Related Related

  1. 1

    "This field cannot be null" error in a django 1.5 ModelForm

  2. 2

    Django - accessing modelform field value in template

  3. 3

    Set value of field for Django ModelForm in CreateView

  4. 4

    Django - Assign default value to field in ModelForm

  5. 5

    Set value of field for Django ModelForm in CreateView

  6. 6

    Django - UpdateView ModelForm setting initial value on queryset field

  7. 7

    Grails number field won't allow blank answer

  8. 8

    Firebase won't bind boolean value to field

  9. 9

    Need a non-editable field in POST data, but ModelForm won't let me

  10. 10

    How to hide a field in django modelform?

  11. 11

    Django ModelForm not showing field errors

  12. 12

    Save custom field in Django ModelForm

  13. 13

    django modelform property hidden field

  14. 14

    JSONField doesn't allow null in Django 3.1

  15. 15

    Allow no whitespace in required form field

  16. 16

    Django | Decimal Field is required even tho i set it to NULL=True

  17. 17

    Django ForeignKey field required despite blank=True and null=True

  18. 18

    Django: Make certain fields in a ModelForm required=False

  19. 19

    Django creation ModelForm with OneToOneField hidden but required

  20. 20

    Django modelform formset first form required

  21. 21

    Django creation ModelForm with OneToOneField hidden but required

  22. 22

    ModelForm won't create instance of model

  23. 23

    Is it ok to use assert on a field I know won't be null?

  24. 24

    Django model field, alternative value return if null?

  25. 25

    Django admin inline calculated field won't save

  26. 26

    django field is required validators

  27. 27

    AngularJS Expression Won't Evaluate in Value Field of Input

  28. 28

    Javascript radio button value won't parse to PHP hidden field

  29. 29

    Can't update django datetime field to Null

HotTag

Archive