castiel
castiel

Reputation: 2773

peculiar thing about leaving the DateTimeField empty in Django?

In the model, I set a DateTimeField's null and blank to be True.

When I try to save the model, it will complain about a time format error when setting that field to an empty string but it will be successful when I give it None.

a.date = ''
a.save() #datetime format error
a.date = None
a.save() #no error this time

Why can't Django auto detect that an empty string is None when I assign it to a field which already allows null and blank?

Upvotes: 3

Views: 2257

Answers (1)

Peter DeGlopper
Peter DeGlopper

Reputation: 37319

DateTimeFields don't accept strings, they accept Python datetime objects. Or None, if set to allow blanks. The empty string isn't a generic Python value for 'no value', so I'm not sure why you would expect it to autoconvert.

Upvotes: 5

Related Questions