Using Forms without Request Data

34 views
Skip to first unread message

Kurtis

unread,
Aug 13, 2012, 4:34:38 PM8/13/12
to django...@googlegroups.com
Hey guys,

This may be a simple question and something I will probably just need more time to dig in and try out. What's the best way to send data to a form (specifically ModelForm) without using request.POST? Should I just build a QueryDict object and send that in?

Thanks!

Melvyn Sopacua

unread,
Aug 13, 2012, 4:55:27 PM8/13/12
to django...@googlegroups.com
On 13-8-2012 22:34, Kurtis wrote:

> This may be a simple question and something I will probably just need more
> time to dig in and try out. What's the best way to send data to a form
> (specifically ModelForm) without using request.POST?

The initial argument work for you?
In short, initial should be used for 'change forms' and request.POST for
the /result/ of that form.
--
Melvyn Sopacua

Kurtis

unread,
Aug 13, 2012, 5:04:36 PM8/13/12
to django...@googlegroups.com
Actually, I'm creating a new object with this form. If it helps to understand the scenario, I'm taking in JSON and creating an object out of the data. But, I can't just pass the raw POST QueryDict because the data doesn't map correctly by any means.

Melvyn Sopacua

unread,
Aug 13, 2012, 5:10:26 PM8/13/12
to django...@googlegroups.com
On 13-8-2012 23:04, Kurtis wrote:
> Actually, I'm creating a new object with this form. If it helps to
> understand the scenario, I'm taking in JSON and creating an object out of
> the data. But, I can't just pass the raw POST QueryDict because the data
> doesn't map correctly by any means.

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.

--
Melvyn Sopacua

Kurtis Mullins

unread,
Aug 13, 2012, 5:14:14 PM8/13/12
to django...@googlegroups.com
On Mon, Aug 13, 2012 at 5:10 PM, Melvyn Sopacua <m.r.s...@gmail.com> 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)

Melvyn Sopacua

unread,
Aug 13, 2012, 5:37:52 PM8/13/12
to django...@googlegroups.com
On 13-8-2012 23:14, Kurtis Mullins wrote:

> 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)
>

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

Kurtis

unread,
Aug 13, 2012, 6:08:11 PM8/13/12
to django...@googlegroups.com
On Monday, August 13, 2012 5:37:52 PM UTC-4, Melvyn Sopacua wrote:

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

Very good points! I do like the error dict but I didn't think about all of the unnecessary other steps it was taking. Thanks a lot, Melvyn!

Kurtis

unread,
Aug 13, 2012, 6:10:03 PM8/13/12
to django...@googlegroups.com
I forgot to mention, I do use the Forms to also exclude certain fields. Maybe a JSON Form class would do me some good here, afterall. The one problem is that my time is limited (budget) so I've got to make sure I don't spend too much time chasing down an optimized happy path. Although, it would definitely be a sound way to go if it doesn't take too much time.

Kurtis Mullins

unread,
Aug 14, 2012, 1:04:04 PM8/14/12
to django...@googlegroups.com
Thanks a lot! I was curious on how to grab the raw POST data but didn't spend any time looking into it. This will definitely come in handy.

On Tue, Aug 14, 2012 at 12:35 PM, S.Prymak <spr...@gmail.com> wrote:
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.

Reply all
Reply to author
Forward
0 new messages