data argument to a form instance must be a dictionary-like object. Other
then that there's no requirements.
I'm kinda curious why you need a form if you're using a non-html data
format.
Hmm. You gain:
- an errors dict
At the cost of:
- form field instance creation
- widget instance creation
You can save some resources by investing in a JSONForm class and I'm
wondering if the following does not do the job:
json_object = json.loads(request.POST['fieldname'])
obj = MyModel(**json_object)
obj.full_clean()
obj.save()
--
Melvyn Sopacua
Here is the code snippet about JSON validation I use in my application:try:json_data = json.loads(request.raw_post_data)except Exception, e:return HttpResponseBadRequest()
form = forms.AttributeForm(json_data)if not form.is_valid():return render_to_json_response({'success': False,'errors': [(k, unicode(v[0])) for k, v in form.errors.items()],'level': messages.DEFAULT_TAGS[messages.ERROR],'data': form.data,})name = form.cleaned_data['name']...This works for me very well. Hope it will work for you too.
вторник, 14 августа 2012 г., 0:14:14 UTC+3 пользователь Kurtis написал:
--You received this message because you are subscribed to the Google Groups "Django users" group.To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/ZOaSBbu8xYQJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.