[Fixed]-Django: DateField "This field cannot be blank."

22πŸ‘

βœ…

Add the blank=True parameter to the definition of your date field if you want that field to be optional.

From the docs:

Note that this is different than null. null is purely database-related, whereas blank is validation-related. If a field has blank=True, form validation will allow entry of an empty value. If a field has blank=False, the field will be required.

7πŸ‘

The first step is to change your field description like so:

date = models.DateField(null=True, blank=True)

null=True is insufficient because that is only a directive relevant to table creation, not to validation. null and blank are separate concepts because there are situations where you only want one and not the other.

By the way, in almost all cases a date and a time field can be compressed into one DateTimeField.

1πŸ‘

It looks like you’re using a library which in turn uses django.forms.ModelForm.

If this is the case, you can add blank=True to your DateField to resolve the issue.

class Event(models.Model):
    title = models.CharField(max_length=200)
    recurring = models.BooleanField()
    day = models.CharField(max_length=20, blank=True)
    date = models.DateField(null=True, blank=True)
    time = models.TimeField()
    description = models.CharField(max_length=500)
    venue = models.CharField(max_length=200, blank=True)
    venueAddress = models.CharField(max_length=200, blank=True)
    venueCity = models.CharField(max_length=200, blank=True)
πŸ‘€Jiaaro

Leave a comment