Created
December 19, 2011 18:54
-
-
Save katjakarhu/1498390 to your computer and use it in GitHub Desktop.
Dajax - Save and edit a single form in a formset
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Like the Dajax full form example, but for a form in a formset: http://dajaxproject.com/fullform/ | |
| # | |
| # Javascript | |
| # get the css id which surrounds the form you need to variable myformcssid | |
| # var data = $(myformcssid + ' *').serializeObject(); # use serialize() if not jQuery | |
| # id for the instance to be modified | |
| # var mymodelid = "{{ form.instance.id }}" | |
| # pass to dajax function | |
| # Dajaxice.questionnaireunit.send_form(Dajax.process, {'data': data, 'myformcssid':myformcssid, 'mymodelid':mymodelid}); | |
| #ajax.py | |
| import re | |
| from django.shortcuts import get_object_or_404 | |
| from dajax.core import Dajax | |
| from dajaxice.decorators import dajaxice_register | |
| from forms import MyForm | |
| from models import MyModel | |
| import collections | |
| def transform_dict(d): # removes form-x- from css id's | |
| out_dict={} | |
| for k,v in d.iteritems(): | |
| k = re.sub("form-\d*-", "", k) | |
| if isinstance(v,collections.MutableMapping): | |
| v=transform_dict(v) | |
| out_dict[k]=v | |
| return out_dict | |
| @dajaxice_register | |
| def send_form(request, data, myformcssid, mymodelid): | |
| dajax = Dajax() | |
| form=transform_dict(data) | |
| obj = get_object_or_404(MyModel, id=mymodelid) | |
| form = MyForm(form, instance=obj) | |
| if form.is_valid(): | |
| #do stuff | |
| form.save() | |
| else: | |
| dajax.alert("This form not is_valid(): %s" % form) | |
| return dajax.json() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment