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 написал:
>
> On Mon, Aug 13, 2012 at 5:10 PM, Melvyn Sopacua 
> <m.r.s...@gmail.com<javascript:>
> > wrote:
>>
>>  
>> 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.
>>
>>
> I figured it out :) It was as simple as creating a QueryDict and sending 
> it in. Thanks for the suggestions!
>
> The reason I'm using a Form (specifically a ModelForm) is to make my job 
> of setting up the Validation a *whole* lot easier.
>
> Here's the code I basically used. Maybe there's a better way to do it?
>
>     json_object = json.loads(request.POST['some_json_field'])
>     q = QueryDict('')
>     q = q.copy()
>     q.update(json_object)
>     form = MyModelForm(q)
>

-- 
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-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to