Opened 12 years ago
Closed 12 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: | dev |
| 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
So if you have, say, a model with just id and a self-referencing FK, and records that look like
you want to be able to say
vals = MyModel.objects.all().values_list('id', 'parent_id', flat=True)and get
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=Trueis 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.