Alejandro Veintimilla
Alejandro Veintimilla

Reputation: 11523

Django. Error saving empty string to DateTimeField(blank=True)

I have a model with a DateTimeField (blank=True, null=True). However, when I try to save an empty string in it, I get the following error:

>>> from courses.models import Event
>>> x = Event.objects.last()
>>> x.date_start = ""
>>> x.save()

django.core.exceptions.ValidationError: ['“” value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format.']

I have never seen this, any ideas? All my migrations are up to date.

Upvotes: 1

Views: 527

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476557

blank=True [Django-doc] has impact on modelforms, not on the model itself. It does not mean that the element accepts an empty string. blank=True simply means that the field is not required. In case the value is not specified, it will use the default value.

But at the model layer, nothing changes. Since your field has null=True [Django-doc], the default in case you do not fill in the value will be None, so you can use:

>>> from courses.models import Event
>>> x = Event.objects.last()
>>> x.date_start = None
>>> x.save()

Upvotes: 2

Related Questions