Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use form values from an unbound form

Tags:

python

django

I have a web report that uses a Django form (new forms) for fields that control the query used to generate the report (start date, end date, ...). The issue I'm having is that the page should work using the form's initial values (unbound), but I can't access the cleaned_data field unless I call is_valid(). But is_valid() always fails on unbound forms.

It seems like Django's forms were designed with the use case of editing data such that an unbound form isn't really useful for anything other than displaying HTML.

For example, if I have:

if request.method == 'GET':
    form = MyForm()
else:
    form = MyForm(request.method.POST)

if form.is_valid():
    do_query(form.cleaned_data['start_date'], form.cleaned_data['end_date'])

is_valid() will fail if this is a GET (since it's unbound), and if I do:

if request.method == 'GET':
    form = MyForm()
    do_query(form.cleaned_data['start_date'], form.cleaned_data['end_date'])
else:
    form = MyForm(request.method.POST)
    if form.is_valid():
       do_query(form.cleaned_data['start_date'], form.cleaned_data['end_date'])

the first call to do_query triggers exceptions on form.cleaned_data, which is not a valid field because is_valid() has not been called. It seems like I have to do something like:

if request.method == 'GET':
    form = MyForm()
    do_query(form['start_date'].field.initial, form['end_date'].field.initial)
else:
    form = MyForm(request.method.POST)
    if form.is_valid():
       do_query(form.cleaned_data['start_date'], form.cleaned_data['end_date'])

that is, there isn't a common interface for retrieving the form's values between a bound form and an unbound one.

Does anyone see a cleaner way to do this?

like image 293
davidavr Avatar asked Sep 16 '08 18:09

davidavr


People also ask

What is an unbound form?

What is a unbound form in Microsoft Access? Basically it is a form that is not bound to any database table or query. You can usually determine if a form (or a control on a form) is bound by looking at its record source property which will either be bound to a Table, Query or SQL String.

Why does my combo box say unbound?

An unbound combo box is just that - it's not bound to any stored value in any table. So selecting a name from the combo box does absolutely nothing! What is the Recordsource for your Grievances form?

What does unbound mean in Microsoft Access?

Unbound control A control that doesn't have a source of data (such as a field or expression) is called an unbound control. You use unbound controls to display information, pictures, lines or rectangles. For example, a label that displays the title of a form is an unbound control.

What is bound and unbound form?

Unbound forms are forms with no data source, whereas bound forms have one and it's the default way to bind your form to your tables or queries.


2 Answers

If you add this method to your form class:

def get_cleaned_or_initial(self, fieldname):
        if hasattr(self, 'cleaned_data'):
            return self.cleaned_data.get(fieldname)
        else:
            return self[fieldname].field.initial

you could then re-write your code as:

if request.method == 'GET':
    form = MyForm()
else:
    form = MyForm(request.method.POST)
    form.is_valid()

do_query(form.get_cleaned_or_initial('start_date'), form.get_cleaned_or_initial('end_date'))
like image 118
Matthew Christensen Avatar answered Oct 13 '22 01:10

Matthew Christensen


Unbound means there is no data associated with form (either initial or provided later), so the validation may fail. As mentioned in other answers (and in your own conclusion), you have to provide initial values and check for both bound data and initial values.

The use case for forms is form processing and validation, so you must have some data to validate before you accessing cleaned_data.

like image 2
zgoda Avatar answered Oct 13 '22 03:10

zgoda