Code

#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

Description

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?

[1] https://github.com/django/django/blob/master/django/db/models/query.py#L542

Attachments (0)

Change History (1)

comment:1 Changed 12 months ago by shai

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to needsinfo
  • Status changed from new to closed

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

id parent_id
1 NULL
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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.