Opened 10 years ago

Closed 9 years ago

Last modified 9 years ago

#1191 closed enhancement (invalid)

[patch] Field type not carried over to manipulator

Reported by: boxed@… Owned by: adrian
Component: Core (Other) Version: master
Severity: normal Keywords:
Cc: fawad@… Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


I wanted to write a custom subclass of FormWrapper and FormFieldWrapper that automatically generates the label tags so instead of writing <label for="id_foo">{% trans "foo" %}{{ foo }} I could just write {{ foo }}. This proved to be impossible since crucial data about the field is not carried over to the manipulator and so cannot be extracted at a later time. I have attached a patch that will make this possible (it's just one line: "man.Klass = klass" in get_manipulator()). After this change I can trivially make the class I wanted to like so:

from django.core import formfields

class FormFieldWrapper(formfields.FormWrapper):
    def __init__(self, formfield, data, error_list, manipulator):
        self.formfield,, self.error_list = formfield, data, error_list
        self.field_name = self.formfield.field_name # for convenience in templates
        self.manipulator = manipulator
    def __str__(self):
        from django.utils.text import capfirst
        from django.core import meta
        opts = self.manipulator.Klass._meta
        field = opts.get_field(self.field_name)
        label_prefix = '<label for="' + self.formfield.get_id()+'">'+capfirst(field.verbose_name)
        label_suffix = '</label>'
        rendered_field = self.formfield.render(
        if isinstance(field, meta.BooleanField):
            return rendered_field + label_prefix+label_suffix+'<br />'
            return label_prefix + ':' + label_suffix + rendered_field+'<br />'
class FormWrapper(formfields.FormWrapper):
    def __getitem__(self, key):
        for field in self.manipulator.fields:
            if field.field_name == key:
                if hasattr(field, 'requires_data_list') and hasattr(, 'getlist'):
                    data =
                    data =, None)
                if data is None:
                    data = ''
                return FormFieldWrapper(field, data, self.error_dict.get(field.field_name, []), self.manipulator)
        raise KeyError

Attachments (1)

addklass.diff (700 bytes) - added by boxed@… 10 years ago.

Download all attachments as: .zip

Change History (3)

Changed 10 years ago by boxed@…


comment:1 Changed 10 years ago by anonymous

  • Cc fawad@… added

comment:2 Changed 9 years ago by Anders Hovmöller <boxed@…>

  • Resolution set to invalid
  • Status changed from new to closed

This is no longer needed in post magic-removal

Note: See TracTickets for help on using tickets.
Back to Top