Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attaching additional information to form fields

I'm trying to pass on additional information to fields of a Django form to be displayed in a template. I tried to override the constructor and add another property to the field like this:

self.fields['field_name'].foo = 'bar'

but in the template this:

{{ form.field_name.foo }}

didn't print anything. Does anyone know how to add additional information to a field without rewriting/inheriting the forms field classes?

like image 643
Max Avatar asked Jul 10 '10 14:07

Max


People also ask

How do I add text to a form?

Open the form or report in Design view by right-clicking the form or report in the Navigation Pane, and then clicking Design View. On the Design tab, in the Controls group, click Text Box. Position the pointer where you want the text box to be placed on the form or report, and then click to insert the text box.

How do I change the interactive form fields in PDF?

To edit a single form field, double-click it or right-click it and choose Properties. To edit multiple form fields, select the fields that you want to edit, right-click one of the selected fields, and choose Properties.

How do I add a field to an existing form in Access?

Add a field to a form or report by using the Field List pane. Open your report in Layout view or Design view. If the Field List pane is not displayed, do one of the following: On the Design tab, in the Tools group, click Add Existing Fields.

What is the purpose of form fields?

Each of these form fields allows the user of the form to select or enter information of the type that you deem appropriate. As an example, let's say you are creating an order form and you need a field where a user can enter the name of the person making the order.


1 Answers

According to django.forms.forms, the __getitem__() method of a Form creates something called a BoundField out of the Field before returning it, thus stripping it of whatever changes you made. If you really want to insert more functionality into that, override that method to do stuff to the bound field before returning it:

class MyForm(forms.Form):
    def __getitem__(self, name):
        boundfield = super(forms.Form,self).__getitem__(name)
        boundfield.foo = "bar"
        return boundfield

Then, "bar" will appear for all fields in that form. You can also make a function and call that instead, to make it more than just a hard-coded string.

While it's more standard to add more fields, or to add properties to the form itself, if you have a whole new class of info that every field needs to contain, this may do it for you.


Another way to get the same thing is to edit an attribute of the field, then access it via the BoundField's "field" attribute:

class MyForm(forms.Form):
    def __init__(self, *args, **kwargs)
        super(forms.Form, self).__init__(*args, **kwargs)
        self.fields['field_name'].foo = "bar"

Then, to access foo in a template:

{{ form.field_name.field.foo }}
like image 50
eruciform Avatar answered Oct 10 '22 02:10

eruciform