flat values() in querysets
The group you are posting to is a
Usenet group . Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
From:
Daniel Poelzleithner <goo... @poelzi.org>
Date: Thu, 03 Aug 2006 20:15:20 +0200
Local: Thurs, Aug 3 2006 2:15 pm
Subject: flat values() in querysets
Hi,
Currently it's kinda verbose to get just a list of id's from a query to put it in a bulk_in or process it further.
example:
>>> NetworkState.objects.all().order_by("-stopdate")[:5].values("id")
[{'id': 2738}, {'id': 2737}, {'id': 2736}, {'id': 2735}, {'id': 2734}]
Then i have to unpack it to get a [2738, 2737,...] list, which is what I wanted in the first place.
I suggest Object.objects.all().values("id", flat=True). Flat as a optional argument, which is only allowed when only one field is requested and returns a list with only the values.
Any better ideas ?
kindly regards Daniel
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
"Adrian Holovaty" <holov... @gmail.com>
Date: Thu, 3 Aug 2006 13:30:32 -0500
Local: Thurs, Aug 3 2006 2:30 pm
Subject: Re: flat values() in querysets
On 8/3/06, Daniel Poelzleithner <goo
... @poelzi.org> wrote:
> >>> NetworkState.objects.all().order_by("-stopdate")[:5].values("id")
> [{'id': 2738}, {'id': 2737}, {'id': 2736}, {'id': 2735}, {'id': 2734}]
> Then i have to unpack it to get a [2738, 2737,...] list, which is what I > wanted in the first place.
> I suggest Object.objects.all().values("id", flat=True). Flat as a > optional argument, which is only allowed when only one field is > requested and returns a list with only the values.
Oooh, that's a nice idea. I'm not 100% certain "flat=True" is the best
name for it, but I can't think of anything better at the moment. Would
you be willing to submit a patch?
Adrian
-- Adrian Holovaty holovaty.com | djangoproject.com
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
"jrs" <goclem... @gmail.com>
Date: Fri, 04 Aug 2006 01:26:32 -0000
Local: Thurs, Aug 3 2006 9:26 pm
Subject: Re: flat values() in querysets
What would the result be if multiple fields were selected? or should
this be valid only when a single field is requested?
NetworkState.objects.all().order_by("-stopdate")[:5].values("id", "name")
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Daniel Poelzleithner <goo... @poelzi.org>
Date: Fri, 04 Aug 2006 03:53:08 +0200
Local: Thurs, Aug 3 2006 9:53 pm
Subject: Re: flat values() in querysets
jrs wrote:
> What would the result be if multiple fields were selected? or should
> this be valid only when a single field is requested?
> NetworkState.objects.all().order_by("-stopdate")[:5].values("id", > "name")
I couldn't think of a nice solution at first, so i said for one key only.
What do you think about returning n tupples for n arguments. if n > 1 and a pure list for only one argument ?
.values("id", "name", flat=True) would return [(id,name),(id,name),...]
.values("id", flat=True) would return [id,id,...]
?
kindly regards
PS: yes i will provide a patch :)
kindly regards Daniel
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
"Adrian Holovaty" <holov... @gmail.com>
Date: Thu, 3 Aug 2006 23:30:08 -0500
Local: Fri, Aug 4 2006 12:30 am
Subject: Re: flat values() in querysets
On 8/3/06, Daniel Poelzleithner <goo
... @poelzi.org> wrote:
> What do you think about returning n tupples for n arguments. if n > 1
> and a pure list for only one argument ?
> .values("id", "name", flat=True) would return > [(id,name),(id,name),...]
> .values("id", flat=True) would return > [id,id,...]
Sounds pretty good to me. Let's do it.
Adrian
-- Adrian Holovaty holovaty.com | djangoproject.com
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
"Gary Wilson" <gary.wil... @gmail.com>
Date: Thu, 03 Aug 2006 22:23:12 -0700
Local: Fri, Aug 4 2006 1:23 am
Subject: Re: flat values() in querysets
Adrian Holovaty wrote: > Oooh, that's a nice idea. I'm not 100% certain "flat=True" is the best > name for it, but I can't think of anything better at the moment. Would > you be willing to submit a patch?
Maybe .flatvalues("id", "name") or .valuelist("id", "name") ?
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
"Will McCutchen" <mccutc... @gmail.com>
Date: Fri, 04 Aug 2006 17:01:05 -0000
Local: Fri, Aug 4 2006 1:01 pm
Subject: Re: flat values() in querysets
Adrian Holovaty wrote: > Sounds pretty good to me. Let's do it.
I opened a ticket and submitted a patch:
http://code.djangoproject.com/ticket/2482 My patch adds a keyword arguement to the 'values' method, as originally proposed. It could be adapted pretty easily to Gary's suggestion of a separate 'flatvalues' or 'valuelist' method.
Will.
You must
Sign in before you can post messages.
You do not have the permission required to post.