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 mardini, 11 years ago

For convenience, Django will automatically create a datetime object 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 for date/time/datetime are defined in that file. A simple error message is shown if you tried to use that feature with an incorrect format.

comment:2 by mardini, 11 years ago

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top