On Friday, January 28, 2011 7:05:21 PM UTC, taso wrote:
>
> Hi,
>
> I am using django strictly as a backend for data processing. As such I
> am not using any forms or templates, just database tables as models.
> The page is js driven and POSTing data is causing me some issues.
>
> A user action triggers a POST that looks like:
>
> action "update"
> data { "field1": 8,"id": 2 }
> (copied from firebug)
>
> When I try to parse the POST data I get errors on every type of method
> I have tried. What I eventually would like to do is something like:
>
> if request.POST.__getitem__('xaction') == 'update':
> mydata = request.POST.__getitem__('data')
> model_to_update = modelname.get(id=mydata['id']
> for k,v in mydata:
> setattr(model_to_update, k, v)
> model_to_update.save()
>
> However every time I try to parse the "data" POST values I get errors.
> I wonder if I am missing some assumptions by not importing/using
> forms, or if there is a standard method to iterate through the POST
> data that I am missing. I have literally tried every \*get\* dict
> method from the docs and all throw errors (.list() throws numeric
> indicies errors, iteritems() throws not iterable, etc.)
>
> Any thoughts?
>
> Thanks
> Taso
>
Why on earth do you want to use __getitem__? That's an internal
implementation detail, which you should never need to access in your code
unless you're overriding something. POST is a dictionary-like object, which
defines __getitem__ so that you can access it with the usual Python
dictionary access: request.POST['data'], and so on.
Secondly, if I'm understanding what you have posted, `data` is not an
element of the POST dictionary - it *is* the POST dictionary. So rather than
trying to access `request.POST['data']['id']`, you should be accessing
`request.POST['id']`.
--
DR.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.