Opened 9 years ago
Last modified 7 years ago
#26634 closed New feature
Add the ability to do subselects for querying versioned data — at Version 1
Reported by: | Darren Hobbs | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.8 |
Severity: | Normal | Keywords: | QuerySet.extra |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
I have versioned data of the form:
id (autogenerate)
ref (uuid)
version (integer)
Each 'save' inserts a new version with the same ref and (version+1) version number.
I'd like an easy way to say 'give me the latest versions' (ie. distinct on refs, with max(version)).
The SQL looks like this:
select * from myapp_project where id in (SELECT id from myapp_project p WHERE p.version = (SELECT max(version) FROM myapp_project p1 WHERE p1.ref = p.ref))
I had to use extra(where=[maxids])
to achieve this*.
*Where maxids = "id in (SELECT id from myapp_project p WHERE p.version = (SELECT max(version) FROM myapp_project p1 WHERE p1.ref = p.ref))"
Stackoverflow discussion here:
http://stackoverflow.com/questions/37303010/what-is-the-django-way-of-doing-a-subselect