Here are the details to reproduce it
'''in a models.py'''
{{{#!python
class Foobar(models.Model):
created = models.DateTimeField(auto_now_add=True)
}}}
'''in a views.py'''
{{{#!python
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
{{{#!python
# 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
--
Ticket URL: <https://code.djangoproject.com/ticket/22878>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* needs_better_patch: => 0
* resolution: => invalid
* needs_tests: => 0
* needs_docs: => 0
Comment:
[https://docs.djangoproject.com/en/dev/ref/models/fields/#datefield 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.
--
Ticket URL: <https://code.djangoproject.com/ticket/22878#comment:1>
Comment (by foxmask):
thank you - i didnt notice this subtility
--
Ticket URL: <https://code.djangoproject.com/ticket/22878#comment:2>
Comment (by Daniel Hahler):
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).
--
Ticket URL: <https://code.djangoproject.com/ticket/22878#comment:3>