Code

Opened 3 years ago

Last modified 10 months ago

#17533 new New feature

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 jezdez)

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 3 years ago.
Or better, set it as a class variable

Download all attachments as: .zip

Change History (9)

Changed 3 years ago by freyley

Or better, set it as a class variable

comment:1 Changed 2 years ago by jezdez

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 2 years ago by jezdez

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

comment:3 Changed 2 years ago by jezdez

  • Triage Stage changed from Unreviewed to Accepted

comment:4 Changed 2 years ago by andrebrantom

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 Changed 2 years ago by kmike

  • Cc kmike84@… added

comment:6 Changed 2 years ago by andrebrantom

  • Cc andrebrantom added

comment:7 Changed 13 months ago by Kamu

  • Version 1.3 deleted

comment:8 Changed 10 months ago by timo

  • 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?

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as new
The owner will be changed from nobody to anonymous. Next status will be 'assigned'
as The resolution will be set. Next status will be 'closed'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.