views:

638

answers:

2

I am using Django and the Google Web Toolkit (GWT) for my current project. I would like to pass a ModelForm instance to GWT via an Http response so that I can "chop" it up and render it as I please. My goal is to keep the form in sync with changes to my models.py file, yet increase control I have over the look of the form. However, the django classes for serialization, serializers and simplejson, cannot serialize a ModelForm. Neither can cPickle. What are my alternatives?

A: 

If your problem is just to serialze a ModelForm to json, just write your own simplejson serializer subclass.

+1  A: 

If you were using pure Django, you'd pass the form to your template, and could then call individual fields on the form for more precise rendering, rather than using ModelForm.to_table. You can use the following to iterate over each field and render it exactly how you want:

{% for field in form.fields %}
    <div class="form-field">{{ field }}</div>
{% endfor %}

This also affords you the ability to do conditional checks using {% if %} blocks inside the loop should you want to exclude certain fields.