Hi All,
I am new to Django and am enjoying it very much.  Excellent documentation, 
tutorials, and general environment to work in.  Good job Django developers!

I have a question on binding data to a ModelFormset.  I have looked 
extensively and not seen this particular issue covered before.

I have a ModelFormset that I send to a template for custom rendering so I 
can present the fields in a controlled tabular format.  The ultimate goal 
is to use CSS to create a red bounding box around any fields with 
validation errors and to set the "title" attribute of those input fields so 
that the user can see what errors were generated  by hovering over the red 
bounded field.  This presents a nice clean interface when there are 
errors.  It all works great, with one annoyance if the model data starts 
out with values that will generate validation errors (i.e. missing required 
data).  

I initially call the view with a GET, which creates the forms and populates 
the form fields.  Since the data is not bound to forms at this point, I 
can't validate and set the errors.  In order to validate the fields and set 
the table error borders, I have to submit the form using POST to bind the 
data, validate the form and generate the errors which ultimately set an 
error_class for styling the table.  This means that I have to "save" the 
from from my form page to get the table style I want.

What I really want is to instantiate a ModelFormset, setting the instance, 
but then to bind the data present in the instance and set errors by sending 
is_valid() to the formset before rendering (i.e. on initial page rendering 
with the GET, so no POST data).  It seemed to me that there should simply 
be a bind_instance() function for all ModelFormsets that contain an 
instance.  I looked and didn't find that.  Eventually, I just decided to 
bind the data myself manually using the following helper function:

# helper functions
def bind_formset(formset):
    bindData={}
    # add management form data
    bindData[formset.get_default_prefix()+"-TOTAL_FORMS"]=str(formset.
management_form['TOTAL_FORMS'].value())
    bindData[formset.get_default_prefix()+"-INITIAL_FORMS"]=str(formset.
management_form['INITIAL_FORMS'].value())
    bindData[formset.get_default_prefix()+"-MIN_NUM_FORMS"]=str(formset.
management_form['MIN_NUM_FORMS'].value())
    bindData[formset.get_default_prefix()+"-MAX_NUM_FORMS"]=str(formset.
management_form['MAX_NUM_FORMS'].value())
    for form in formset:
        if form.instance:
            for fieldName,fieldValue in form.fields.iteritems():
                try:
                    bindData[form.add_prefix(fieldName)]=getattr(form.
instance,fieldName)
                except:
                    # this is an added field, not derived from the model
                    pass
    newFormset=formset.__class__(bindData,instance=formset.instance,
                                  queryset=formset.queryset, error_class=
formset.error_class)
    return newFormset

This works!  My question... Is this a reasonable approach?  Or did I just 
miss the obvious way of doing this?

Thanks for any help!

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/ad0edd7f-b8ea-42c1-9d2f-4ef836bba703%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to