Opened 3 years ago

Closed 3 years ago

#20719 closed New feature (needsinfo)

permit values_list flat with more than one key

Reported by: rm_ Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords: values_list flat
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


As far as i understand the limitation of flat with just one attribute looks arbitrary [1] likely to prevent one to shoot himself in the foot getting data of different types / or different meaning. But when you have a model with a ForeignKey to itself that is handy. Some popular application like django-mptt use this kind of models.
If the change is sound, I think i can have a try at doing a patch, should one add a force_flat flag or just remove the limitation and update documentation?


Change History (1)

comment:1 Changed 3 years ago by Shai Berger

Resolution: needsinfo
Status: newclosed

So if you have, say, a model with just id and a self-referencing FK, and records that look like

id parent_id
2 1
3 1
4 2

you want to be able to say

vals = MyModel.objects.all().values_list('id', 'parent_id', flat=True)

and get

vals == [1, None, 2, 1, 3, 1, 4, 2]

instead of the current

vals = MyModel.objects.all().values_list('id', 'parent_id')
vals == [(1, None), (2, 1), (3, 1), (4, 2)]

I'd be -0 at best. The current flat=True is a shortcut for a very common use case. This isn't, and getting it right is a little more complicated than removing the limitation at the API.

I'm closing this on needsinfo, so you may reopen if you have a more convincing explanation of the feature and its usefulness.

Note: See TracTickets for help on using tickets.
Back to Top