title: Django forms, inheritance and order of form fields
body: <p>I'm using Django forms in my website and would like to control the order of the fields.</p>
  
  <p>Here's how I define my forms:</p>
  
  <pre><code>class edit_form(forms.Form):
      summary = forms.CharField()
      description = forms.CharField(widget=forms.TextArea)
  
  
  class create_form(edit_form):
      name = forms.CharField()
  </code></pre>
  
  <p>The name is immutable and should only be listed when the entity is created. I use inheritance to add consistency and DRY principles. What happens which is not erroneous, in fact totally expected, is that the name field is listed last in the view/html but I'd like the name field to be on top of summary and description. I do realize that I could easily fix it by copying summary and description into create_form and loose the inheritance but I'd like to know if this is possible. </p>
  
  <p><strong>Why?</strong> Imagine you've got 100 fields in edit_form and have to add 10 fields on the top in create_form - copying and maintaining the two forms wouldn't look so sexy then. (This is <em>not</em> my case, I'm just making up an example)</p>
  
  <p>So, how can I override this behavior?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Apparently there's no proper way to do this without going through nasty hacks (fiddling with .field attribute). The .field attribute is a SortedDict (one of Django's internal datastructures) which doesn't provide any way to reorder key:value pairs. It does how-ever provide a way to insert items at a given index but that would move the items from the class members and into the constructor. This method would work, but make the code less readable. The only other way I see fit is to modify the framework itself which is less-than-optimal in most situations.</p>
  
  <p>In short the code would become something like this:</p>
  
  <pre><code>class edit_form(forms.Form):
      summary = forms.CharField()
      description = forms.CharField(widget=forms.TextArea)
  
  
  class create_form(edit_form):
      def __init__(self,*args,**kwargs):
          forms.Form.__init__(self,*args,**kwargs)
  
          self.fields.insert(0,'name',forms.CharField())
  </code></pre>
  
  <p>That shut me up :) </p>