Opened 18 years ago

Closed 18 years ago

Last modified 18 years ago

#1191 closed enhancement (invalid)

[patch] Field type not carried over to manipulator

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

Description

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.data, 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(self.data)
        if isinstance(field, meta.BooleanField):
            return rendered_field + label_prefix+label_suffix+'<br />'
        else:
            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(self.data, 'getlist'):
                    data = self.data.getlist(field.field_name)
                else:
                    data = self.data.get(field.field_name, 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@… 18 years ago.
patch

Download all attachments as: .zip

Change History (3)

by boxed@…, 18 years ago

Attachment: addklass.diff added

patch

comment:1 by anonymous, 18 years ago

Cc: fawad@… added

comment:2 by Anders Hovmöller <boxed@…>, 18 years ago

Resolution: invalid
Status: newclosed

This is no longer needed in post magic-removal

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