Created
May 27, 2011 10:32
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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