I have a form somewhere:
class FooForm(forms.Form):
a_field = forms.CharField()
not_a_field = 'hello world'
When instantiating this, and trying to access the field, I get a
>>> from baz.views import FooForm
>>> f = FooForm()
>>> f.not_a_field
'hello world'
>>> f.a_field
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'FooForm' object has no attribute 'a_field'
What is going on?
The way that
Formis designed, the attributes are removed as attributes and added to thefieldsattribute. This is done so that we can use declarative syntax to define ourForm, but can loop through the fields when the time comes.You can find the logic for this in two places.
django/forms/forms.pycontains the classDeclarativeFieldsMetaclassand the methodget_declared_fields. These two bits contain the logic to move everything into thefieldslist.