0

Per this question: Difference between Django Form 'initial' and 'bound data'? :

Here's the key part from the django docs on bound and unbound forms.

If it’s bound to a set of data, it’s capable of validating that data and rendering the form as HTML with the data displayed in the HTML.

If it’s unbound, it cannot do validation (because there’s no data to validate!), but it can still render the blank form as HTML.

My question is: Is there an easy way to know which fields need to be bound in order to validate?

We have a multi-inheritance ModelForm nightmare and it's really tough to figure out which what are the minimum required fields to "bind".

In this case I've tried matching my form.data to vars(form.fields), but this is not enough, it's just ongoing potluck tracking back through the models and adding more and more to the form.data in an ad-hoc way.

Is there some cardinal list somewhere of the minimum requirements of bindable fields?

Community
  • 1
  • 1
Williams
  • 4,044
  • 1
  • 37
  • 53
  • Your question is a bit odd; because there is no such thing as "bindable field". A bound form just means there is _some_ data added to it; the data may not be complete - that is, it may not represent all fields in the form; and thats perfectly fine. What is the actual problem you are trying to solve? – Burhan Khalid Aug 08 '13 at 03:55
  • Thanks Burhan, there is _some_ data, but the form is still `form.is_bound=False`. What I want to know is what more I need to add to the data in order for `is_bound` to be True. – Williams Aug 08 '13 at 04:03
  • Can you post some code? It would be much easier to figure out what is going on. – Burhan Khalid Aug 08 '13 at 04:06

1 Answers1

0

Based on your clarifying comment:

[T]here is some data, but the form is still form.is_bound=False. What I want to know is what more I need to add to the data in order for is_bound to be True.

It's not a question of what data is present or absent, it's a question of how you construct the form object.

From the docs:

To create an unbound Form instance, simply instantiate the class:

>>> f = ContactForm()

To bind data to a form, pass the data as a dictionary as the first parameter to your Form class constructor:
>>> data = {'subject': 'hello',
...         'message': 'Hi there',
...         'sender': 'foo@example.com',
...         'cc_myself': True}
>>> f = ContactForm(data)

That is, if you provided an argument when instantiating the form, it's bound. If not, not.

Peter DeGlopper
  • 36,326
  • 7
  • 90
  • 83
  • Ah! Thank you. I was adding data post-hoc and apparently this was what was making it seem as though the form was unbound. – Williams Aug 08 '13 at 05:11