#26634: Add the ability to do subselects for querying versioned data
-------------------------------+----------------------------
     Reporter:  dhobbs         |      Owner:  nobody
         Type:  Uncategorized  |     Status:  new
    Component:  Uncategorized  |    Version:  1.8
     Severity:  Normal         |   Keywords:  QuerySet.extra
 Triage Stage:  Unreviewed     |  Has patch:  0
Easy pickings:  0              |      UI/UX:  0
-------------------------------+----------------------------
 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

--
Ticket URL: <https://code.djangoproject.com/ticket/26634>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/049.d9b5ca5d83199ff302b739eafceaa875%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to