#11765 closed (invalid)
models.DateField(null=True, blank=True) accepts None, but not the empty string '' — at Version 4
| Reported by: | shmengie | Owned by: | nobody |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | yes | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
For some reason I cannot create a record with
class MyModel(models.Model):
models.DateField(null=True, blank=True)
MyModel(MyDateField='')
It must be either None or a valid date.
Change History (4)
comment:1 by , 16 years ago
| Summary: | models.DateField(null=True, blank=True) accepts None, but not null string '' → models.DateField(null=True, blank=True) accepts None, but not the empty string '' |
|---|
follow-up: 4 comment:2 by , 16 years ago
| Component: | Uncategorized → Database layer (models, ORM) |
|---|---|
| Needs tests: | set |
| Version: | 1.1 → SVN |
comment:3 by , 16 years ago
| Resolution: | → invalid |
|---|---|
| Status: | new → closed |
blank=True tells user that they don't have to fill this field, and it doesn't mean that its empty string. Django assign NoneType if empty.
comment:4 by , 16 years ago
| Description: | modified (diff) |
|---|
Replying to mrts:
The problem is that
DateField.to_python()(orDateTimeField) doesn't treat''specially...
Neither does IntegerField nor FloatField, eg. It's not clear to me why Date fields would/should be singled out for special treatment here. Forms fields consistently handle treating empty strings input as None, but if you are operating at the model level then you need to use None when you mean None, not an empty string.
The problem is that
DateField.to_python()(orDateTimeField) doesn't treat''specially, see source:/django/trunk/django/db/models/fields/__init__.py@10545#L464class DateTest(models.Model): foo = models.DateField(null=True, blank=True)>>> d = DateTest(foo='') >>> d.save() Traceback (most recent call last): ... File "/usr/lib/python2.6/dist-packages/django/db/models/fields/__init__.py", line 474, in to_python _('Enter a valid date in YYYY-MM-DD format.')) ValidationError: Enter a valid date in YYYY-MM-DD format.