Opened 13 years ago

Closed 9 years ago

#17533 closed New feature (duplicate)

When attempting to build more complex widgets/fields, access to form data would be helpful

Reported by: freyley Owned by: nobody
Component: Forms Version:
Severity: Normal Keywords:
Cc: kmike84@…, andrebrantom Triage Stage: Accepted
Has patch: yes Needs documentation: yes
Needs tests: yes Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description (last modified by Jannis Leidel)

I'm currently working on building more complex fields with multi-html-element widgets, and connecting them to incoming data models in a way that I think isn't doable in the current design of forms. However, there is a place where fields have access to form data, and that's in the BoundField class in forms.py. If I build my own BoundField class, I can easily give my fields access to the data they need when rendering the widget, as the value() method has access to self.form and self.field. Unfortunately, to build my own BoundField class I have to implement a number of functions in the BaseForm class, one of which, _html_output, is long and not something I actually want to maintain a different version of.

So I propose a minor modification: have BaseForm.__init__ set a self._bound_field_class and then use that everywhere, which allows Form class derivatives to set a different BoundField class.

I am attaching a patch which does just that. It is against 1.3.1

Attachments (1)

boundfield.patch (1.6 KB ) - added by freyley 13 years ago.
Or better, set it as a class variable

Download all attachments as: .zip

Change History (10)

by freyley, 13 years ago

Attachment: boundfield.patch added

Or better, set it as a class variable

comment:1 by Jannis Leidel, 13 years ago

Description: modified (diff)

comment:2 by Jannis Leidel, 13 years ago

Accepting in principal since this might be useful if we'd add template based widget rendering.

comment:3 by Jannis Leidel, 13 years ago

Triage Stage: UnreviewedAccepted

comment:4 by andrebrantom, 13 years ago

I just ran into the solution specified here, but with a slightly different use-case, which is to be able to pass the field's label to the widget so that it can render everything at once (Bootstrap 2 has the notion of control-group for which it makes sense to render it in a single go). The final label is frustratingly only available in the BoundField instance, since that's the only place where the Field and it's declared name come together.

So +1 on this!

comment:5 by Mikhail Korobov, 13 years ago

Cc: kmike84@… added

comment:6 by andrebrantom, 13 years ago

Cc: andrebrantom added

comment:7 by Kamu, 11 years ago

Version: 1.3

comment:8 by Tim Graham, 11 years ago

Easy pickings: unset
Needs documentation: set
Needs tests: set
Patch needs improvement: set

The proposed patch is simple enough, but if there are I'm wondering if it would be better to make this a public API with docs and tests?

comment:9 by Tim Graham, 9 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #25294 which has a more thorough patch.

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