Opened 13 years ago
Closed 13 years ago
#16583 closed Bug (wontfix)
Explicitly passing a date to a DateTimeField in a model's create method should override auto_now_add
Reported by: | ian | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.3 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I'm writing some unit tests where I'm testing specific logic based on DateTimeFields in a model. When I use the .create() method on the model, I pass in a specific datetime to the DateTimeField but it gets ignored and overwritten by the auto_now_add functionality.
The only way I can do what I want is by first creating, then altering the value on the DateTimeField, then saving a second time to override.
Change History (5)
comment:1 by , 13 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:2 by , 13 years ago
Resolution: | invalid |
---|---|
Status: | closed → reopened |
I realize it works this way, but I don't agree that it is the correct behaviour. Based on PEP8, I would claim that this is being implicit, rather than explicit, and not pythonic. Any other field that has a default can be overridden.
No explanation as to why the behaviour is as it is, only that it is, and pointing me to the documentation does NOT make it correct.
comment:4 by , 13 years ago
Sorry for so many comment, but this is a normal situation where I've used auto_now_add and I want to run unit tests against the model. To say that I need to override save() seems utterly ridiculous simply to satisfy my testing situation.
The behaviour is unexpected, and is therefore called into question. Please do not simply dismiss it based on what the docs say.
comment:5 by , 13 years ago
Resolution: | → wontfix |
---|---|
Status: | reopened → closed |
As aaugustin pointed out, this is a known behavior of auto_now_add
and can be prevented by providing a custom save method and using the default argument. That way you're actually receiving the behavior you expected.
Per the docs:
You can think of
auto_now_add
(andauto_now
) as database triggers.In your situation, you shouldn't use
auto_now_add
. Instead you should overridesave()
like this: