#23805: query `first` method clears cached queryset for prefetch_related
-------------------------------------+-------------------------------------
     Reporter:  alecklandgraf        |                    Owner:  nobody
         Type:  Uncategorized        |                   Status:  new
    Component:  Database layer       |                  Version:  master
  (models, ORM)                      |               Resolution:
     Severity:  Normal               |             Triage Stage:
     Keywords:  first query          |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by tchaumeny):

 * needs_better_patch:   => 0
 * needs_tests:   => 0
 * needs_docs:   => 0


Comment:

 In the first code example, `first()` is used on an unordered queryset. As
 `first()` is intended to be deterministic, it isn't surprising that it
 tries to add an order condition (which default to `order_by('pk')`.

 Your second code example might work, but it isn't deterministic either as
 there is no guarantee on the ordering of `pizza.toppings.all()`.

 Do you need an arbitrary element or the first one ? If you need the first
 one, there should be some notion of ordering involved, otherwise your
 second solution is fine.

 Also, you might wanna have a look at
 
https://docs.djangoproject.com/en/1.7/ref/models/queries/#django.db.models.Prefetch
 (I've never used it myself but it should allow more complex
 prefetch_related queryset, including ORDER clause as described here
 https://docs.djangoproject.com/en/1.7/ref/models/querysets/#prefetch-
 related).

--
Ticket URL: <https://code.djangoproject.com/ticket/23805#comment:1>
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/071.8b4e89745798bb596411c4133b08bfa7%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to