#22878 closed Bug (invalid)
DateTimeField auto_now_add, auto_now & CreateView
Reported by: | FoxMaSk | Owned by: | nobody |
---|---|---|---|
Component: | Uncategorized | Version: | 1.6 |
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
Hi,
I think I spotted a bug with auto_now_add and auto_now in the models.
Here are the details to reproduce it
in a models.py
class Foobar(models.Model): created = models.DateTimeField(auto_now_add=True)
in a views.py
class FoobarCreateView(CreateView): model = Foobar fields = [ 'created' ]
will produce the error :
Unknown field(s) created specified for Foobar
To try to trakc the error, I put a "print fields" line 285 of the file django/forms/models.py in the class ModelFormMetaclass just before
# make sure opts.fields doesn't specify an invalid field none_model_fields = [k for k, v in six.iteritems(fields) if not v]
which gives :
{u'created': None}
if I drop the auto_now_add from the model I get :
u'created': <django.forms.fields.DateTimeField object at 0x23bca10>
and get no error
The exact same behavior occurs with auto_now
Regards
Change History (3)
comment:1 by , 10 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:3 by , 5 years ago
It can be useful to use editable=True
to have it included in admin forms by default (where the whole form might be readonly then).
If the current behavior is kept it should probably throw a TypeError
if editable=True
or `blank=False´ is passed instead of silently overriding/ignoring it, which causes confusion e.g. with django-extensions (https://github.com/django-extensions/django-extensions/issues/745).
As documented, "As currently implemented, setting auto_now or auto_now_add to True will cause the field to have editable=False and blank=True set." Since the field isn't editable, you can't add it to a form.