Opened 11 years ago
Closed 11 years ago
#22755 closed Bug (invalid)
Problem with date fields
| Reported by: | anonymous | Owned by: | nobody |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 1.6 |
| Severity: | Normal | Keywords: | DateField |
| Cc: | kamil@… | Triage Stage: | Unreviewed |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Hi.
I found a strange problem with datefield in models. For example we have:
class MyModel(models.Model):
date = models.DateField(verbose_name=_('Date'), default=datetime.datetime.now)
And now we have one object in db with date 2014-06-03 (today). And now i am tring filter objects:
MyModel.objects.filter(date=date.today()) # Works MyModel.objects.filter(date='2014-06-03') # Also works MyModel.objects.filter(date='03-06-2014') # Validation error [u"'03-06-2014' value has an invalid date format. It must be in YYYY-MM-DD format."]
In my opinion, this behavior is not correct. Comparing date and string leads to errors. Why DateField accepts a string as a date?
Change History (2)
comment:1 by , 11 years ago
comment:2 by , 11 years ago
| Resolution: | → invalid |
|---|---|
| Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
For convenience, Django will automatically create a
datetimeobject from a string if you need one in your fields (like your example).This is done in
utils/dateparse.py, and the accepted regular expressions fordate/time/datetimeare defined in that file. A simple error message is shown if you tried to use that feature with an incorrect format.