If I have a Django form such as:
class ContactForm(forms.Form):
subject = forms.CharField(max_length=100)
message = forms.CharField()
sender = fo
I went ahead and answered my own question. Here's the answer for future reference:
In Django form.py
does some dark magic using the __new__
method to load your class variables ultimately into self.fields
in the order defined in the class. self.fields
is a Django SortedDict
instance (defined in datastructures.py
).
So to override this, say in my example you wanted sender to come first but needed to add it in an init method, you would do:
class ContactForm(forms.Form):
subject = forms.CharField(max_length=100)
message = forms.CharField()
def __init__(self,*args,**kwargs):
forms.Form.__init__(self,*args,**kwargs)
#first argument, index is the position of the field you want it to come before
self.fields.insert(0,'sender',forms.EmailField(initial=str(time.time())))