Ticket #5986: django-fields-weight.patch

File django-fields-weight.patch, 1.9 KB (added by emes, 8 years ago)

The patch adding weight parameter to newforms.Field

  • newforms/fields.py

     
    5050    creation_counter = 0
    5151
    5252    def __init__(self, required=True, widget=None, label=None, initial=None,
    53                  help_text=None, error_messages=None):
     53                 help_text=None, error_messages=None, weight=0):
    5454        # required -- Boolean that specifies whether the field is required.
    5555        #             True by default.
    5656        # widget -- A Widget class, or instance of a Widget class, that should
     
    6464        # initial -- A value to use in this Field's initial display. This value
    6565        #            is *not* used as a fallback if data isn't given.
    6666        # help_text -- An optional string to use as "help text" for this Field.
     67        # weight -- An optional parameter to control Fields order in a Form.
    6768        if label is not None:
    6869            label = smart_unicode(label)
    6970        self.required, self.label, self.initial = required, label, initial
     
    8384        self.creation_counter = Field.creation_counter
    8485        Field.creation_counter += 1
    8586
     87        self.weight = weight
     88
    8689        self.error_messages = self._build_error_messages(error_messages)
    8790
    8891    def _build_error_messages(self, extra_error_messages):
  • newforms/forms.py

     
    3737        for base in bases[::-1]:
    3838            if hasattr(base, 'base_fields'):
    3939                fields = base.base_fields.items() + fields
     40       
     41        # Field weights override declaration and inheritance order.
     42        fields.sort(lambda x, y: cmp(x[1].weight, y[1].weight))
    4043
    4144        attrs['base_fields'] = SortedDict(fields)
    4245        return type.__new__(cls, name, bases, attrs)
Back to Top