QuerySet.values() for single object?

5,293 views
Skip to first unread message

Shadow

unread,
May 8, 2009, 12:23:41 AM5/8/09
to Django users
Hi,

If i get a model object with Model.objects.get()

Is their a way to turn that into a dictionary?

Thanks

George Song

unread,
May 8, 2009, 2:01:53 AM5/8/09
to django...@googlegroups.com
On 5/7/2009 9:23 PM, Shadow wrote:
> If i get a model object with Model.objects.get()
>
> Is their a way to turn that into a dictionary?

Why not just filter for it and use values()?

--
George

Alex Gaynor

unread,
May 8, 2009, 3:38:22 AM5/8/09
to django...@googlegroups.com
You can do .filter(stuff).values(fields_list).get() and it will return a dictionary.

Alex

--
"I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire
"The people's good is the highest law."--Cicero

K*K

unread,
May 8, 2009, 5:03:35 AM5/8/09
to Django users
Simple way:

>>> model = Model.objects.get(id = 1)
>>> print model.__dict__

Shadow

unread,
May 8, 2009, 12:47:25 PM5/8/09
to Django users
On May 8, 7:03 pm, "K*K" <XuqingKu...@gmail.com> wrote:
> Simple way:
>
> >>> model = Model.objects.get(id = 1)
> >>> print model.__dict__
>

Awesome, thanks!

I did try "dict(object)" :P

btw: I didn't want to use a filter because it was all nicely wrapped
in:

get_object_or_404(model, id=x)
Reply all
Reply to author
Forward
0 new messages