Ticket #5986: django-fields-order.3.patch

File django-fields-order.3.patch, 5.6 KB (added by Patryk Zawadzki <patrys@…>, 17 years ago)

Added support for form_for_model

  • django/newforms/forms.py

     
    88from django.utils.html import escape
    99from django.utils.encoding import StrAndUnicode, smart_unicode, force_unicode
    1010from django.utils.safestring import mark_safe
     11from django.core.management.base import CommandError
    1112
    1213from fields import Field
    1314from widgets import TextInput, Textarea
     
    3839            if hasattr(base, 'base_fields'):
    3940                fields = base.base_fields.items() + fields
    4041
     42        # If this class is created by form_for_instance or form_for_model
     43        # base_fields are passed in attrs
     44        if attrs.has_key('base_fields'):
     45            fields = attrs['base_fields'].items() + fields
     46
     47        # Sort by Meta.fields_order if present.
     48        # Raises CommandError if any of the fields is missing.
     49        if attrs.has_key('Meta') and hasattr(attrs['Meta'], 'fields_order'):
     50            for f in fields:
     51                if not f[0] in attrs['Meta'].fields_order:
     52                    raise CommandError, "%s not present in %s.Meta.fields_order" % (f[0], name)
     53            fields.sort(lambda x, y: cmp(attrs['Meta'].fields_order.index(x[0]), attrs['Meta'].fields_order.index(y[0])))
     54
    4155        attrs['base_fields'] = SortedDict(fields)
    4256        return type.__new__(cls, name, bases, attrs)
    4357
  • tests/regressiontests/forms/fields_order.py

     
     1# -*- coding: utf-8 -*-
     2
     3tests = r"""
     4>>> from django.newforms import *
     5>>> class TestForm(Form):
     6...     f1=CharField(max_length=10)
     7>>> TestForm(auto_id=False).as_p()
     8u'<p>F1: <input type="text" name="f1" maxlength="10" /></p>'
     9>>> class TestForm(Form):
     10...     f1=CharField(max_length=10)
     11...     class Meta:
     12...         fields_order=['baz']
     13Traceback (most recent call last):
     14...
     15CommandError: f1 not present in TestForm.Meta.fields_order
     16>>> class TestForm(Form):
     17...     f1=CharField(max_length=10)
     18...     f2=CharField(max_length=10)
     19...     class Meta:
     20...         fields_order=['f1']
     21Traceback (most recent call last):
     22...
     23CommandError: f2 not present in TestForm.Meta.fields_order
     24>>> class TestForm(Form):
     25...     f1=CharField(max_length=10)
     26...     class Meta:
     27...         fields_order=['f1']
     28>>> TestForm(auto_id=False).as_p()
     29u'<p>F1: <input type="text" name="f1" maxlength="10" /></p>'
     30>>> class TestForm(Form):
     31...     f1=CharField(max_length=10)
     32...     f2=CharField(max_length=10)
     33...     class Meta:
     34...         fields_order=['f1','f2']
     35>>> TestForm(auto_id=False).as_p()
     36u'<p>F1: <input type="text" name="f1" maxlength="10" /></p>\n<p>F2: <input type="text" name="f2" maxlength="10" /></p>'
     37>>> class TestForm(Form):
     38...     f1=CharField(max_length=10)
     39...     f2=CharField(max_length=10)
     40...     class Meta:
     41...         fields_order=['f2','f1']
     42>>> TestForm(auto_id=False).as_p()
     43u'<p>F2: <input type="text" name="f2" maxlength="10" /></p>\n<p>F1: <input type="text" name="f1" maxlength="10" /></p>'
     44>>> class SomeForm(TestForm):
     45...     f3=CharField(max_length=10)
     46>>> TestForm(auto_id=False).as_p()
     47u'<p>F2: <input type="text" name="f2" maxlength="10" /></p>\n<p>F1: <input type="text" name="f1" maxlength="10" /></p>'
     48>>> SomeForm(auto_id=False).as_p()
     49u'<p>F2: <input type="text" name="f2" maxlength="10" /></p>\n<p>F1: <input type="text" name="f1" maxlength="10" /></p>\n<p>F3: <input type="text" name="f3" maxlength="10" /></p>'
     50>>> class SomeForm(TestForm):
     51...     f3=CharField(max_length=10)
     52...     class Meta:
     53...         fields_order=['f3']
     54Traceback (most recent call last):
     55...
     56CommandError: f2 not present in SomeForm.Meta.fields_order
     57>>> class SomeForm(TestForm):
     58...     f3=CharField(max_length=10)
     59...     class Meta:
     60...         fields_order=['f3','f2','f1']
     61>>> SomeForm(auto_id=False).as_p()
     62u'<p>F3: <input type="text" name="f3" maxlength="10" /></p>\n<p>F2: <input type="text" name="f2" maxlength="10" /></p>\n<p>F1: <input type="text" name="f1" maxlength="10" /></p>'
     63>>> from django.contrib.admin.models import User
     64>>> SomeForm = form_for_model(User, Form, fields = ['username'])
     65>>> class TestForm(SomeForm):
     66...     f1 = CharField(max_length=10)
     67...     class Meta:
     68...         fields_order=['f1','username']
     69>>> TestForm(auto_id=False).as_p()
     70u'<p>F1: <input type="text" name="f1" maxlength="10" /></p>\n<p>Username: <input type="text" name="username" maxlength="30" /> Required. 30 characters or fewer. Alphanumeric characters only (letters, digits and underscores).</p>'
     71"""
  • tests/regressiontests/forms/tests.py

     
    22from extra import tests as extra_tests
    33from fields import tests as fields_tests
    44from forms import tests as form_tests
     5from fields_order import tests as fields_order_tests
    56from error_messages import tests as custom_error_message_tests
    67from localflavor.ar import tests as localflavor_ar_tests
    78from localflavor.au import tests as localflavor_au_tests
     
    2930__test__ = {
    3031    'extra_tests': extra_tests,
    3132    'fields_tests': fields_tests,
     33    'fields_order_test': fields_order_tests,
    3234    'form_tests': form_tests,
    3335    'custom_error_message_tests': custom_error_message_tests,
    3436    'localflavor_ar_tests': localflavor_ar_tests,
Back to Top