flat values() in querysets

ยอดดู 18 ครั้ง
ข้ามไปที่ข้อความที่ยังไม่อ่านรายการแรก

Daniel Poelzleithner

ยังไม่อ่าน,
3 ส.ค. 2549 14:15:203/8/49
ถึง django-d...@googlegroups.com
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

Adrian Holovaty

ยังไม่อ่าน,
3 ส.ค. 2549 14:30:323/8/49
ถึง django-d...@googlegroups.com
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

jrs

ยังไม่อ่าน,
3 ส.ค. 2549 21:26:323/8/49
ถึง Django developers
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")

Daniel Poelzleithner

ยังไม่อ่าน,
3 ส.ค. 2549 21:53:083/8/49
ถึง django-d...@googlegroups.com
jrs wrote:

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

Adrian Holovaty

ยังไม่อ่าน,
4 ส.ค. 2549 00:30:084/8/49
ถึง django-d...@googlegroups.com
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.

Gary Wilson

ยังไม่อ่าน,
4 ส.ค. 2549 01:23:124/8/49
ถึง Django developers
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") ?

Will McCutchen

ยังไม่อ่าน,
4 ส.ค. 2549 13:01:054/8/49
ถึง Django developers
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.

ตอบทุกคน
ตอบกลับผู้สร้าง
ส่งต่อ
ข้อความใหม่ 0 รายการ