Opened 13 years ago
Closed 13 years ago
#16312 closed New feature (wontfix)
models.DateField() doesn't convert a datetime.datetime() to datetime.date()
Reported by: | jedie | Owned by: | nobody |
---|---|---|---|
Component: | Uncategorized | Version: | dev |
Severity: | Normal | Keywords: | DateField |
Cc: | Triage Stage: | Design decision needed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
IMHO models.DateField() should convert a given datetime.datetime() to datetime.date(). It makes this in DateField.to_python(), but the model value is a datetime.datetime()
e.g.:
class Foo(models.Model): date = models.DateField() a = Foo(date=datetime.date(year=2001, month=1, day=1)) # returns a datetime.date(), ok. a = Foo(date=datetime.datetime(year=2001, month=1, day=1)) # Failure: returns a datetime.datetime() and not a date
Change History (4)
comment:1 by , 13 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 13 years ago
Triage Stage: | Accepted → Design decision needed |
---|---|
Type: | Bug → New feature |
comment:4 by , 13 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
Auto-"conversion" of values paves the way for silent data corruption, that isn't a good idea. (I actually think Django is a bit laxist in its to_python
methods.) Strict type checking isn't good either — Python favors duck typing.
Since DateField has historically accepted datetimes (and DateTimeField accepted dates), there's not much we can do without breaking backwards compatibility.
zuber is right: an isolated change to DateField isn't the way to go. If you want to work on this, please send a sufficiently detailed proposal to django-developers, summarizing the general problem and addressing backwards compatibility.
I sympathise with the reporter as the automatic conversion of values assigned to
DateField
andDateTimeField
to the underlying type would certainly reduce the number of bugs in our codebase.Unfortunately that's a more general issue, as all the fields bundled with Django don't perform any validation or conversion on assignment (look at
BooleanField
andIntegerField
for a simple example). The validation and conversion is performed only on saving and loading data from database. Changing only the behavior ofDateField
andDateTimeField
would introduce an inconsistency and changing the behavior of all fields would break a lot of code in the wild.I'm setting this ticket to design decision needed as this is a nice feature proposal, but it introduces a major backwards incompatibility.