Skip to content

Instantly share code, notes, and snippets.

@dokterbob
Created May 27, 2011 10:32
Show Gist options
  • Save dokterbob/995013 to your computer and use it in GitHub Desktop.
Save dokterbob/995013 to your computer and use it in GitHub Desktop.
Form mixin class which allows for reordering and hiding fields for normal forms, similar to the way this is possible with ModelForms.
class OrderableFormMixin(object):
"""
Form mixin class which allows for reordering and hiding fields for normal
forms, similar to the way this is possible with ModelForms::
class MyFunkyForm(OrderableFormMixin, Form):
class Meta:
fields = ('my_field1', 'my_field2')
This snippet is based on:
https://code.djangoproject.com/wiki/CookBookNewFormsFieldOrdering
http://stackoverflow.com/questions/913589/django-forms-inheritance-and-order-of-form-fields
"""
def __init__(self, *args, **kwargs):
super(OrderableFormMixin, self).__init__(*args, **kwargs)
# Get the Meta class, if available
meta_class = getattr(self, 'Meta', None)
if meta_class:
fields_list = getattr(meta_class, 'fields', None)
if fields_list:
self.fields.keyOrder = fields_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment