On Fri, Jul 4, 2014 at 4:42 PM, Jon Dufresne <jon.dufre...@gmail.com> wrote:

> Suppose I have the following models:
>
> ---
> class Container(models.Model):
>     pass
>
> class Item(models.Model):
>     container = models.ForeignKey(Container)
>     previous = models.ForeignKey('self', null=True)
>     current = models.BooleanField()
>     flag = models.BooleanField()
> ---
>

[snip]


> A slow query is better than no query at all. Any particular reason
> this was removed? How would one model and query what I'm trying to
> achieve moving forward to avoid inefficient queries and exceptions?
>

Two ways come to mind:

* If it's only "flag" on the first item in a sequence then move "flag" to
be on present on Container also. A "first_flag" field. Could manage in a
signal or override Item.save(). That would let you not nest the query, and
could be more efficient.

* Build a list of relevant containers first:
Container.objects.filter(item__previous__isnull=True,
item__flag=True).values_list('pk', flat=True). Then
Items.objects.filter(current=True, container__in=flagged_containers). Not a
problem as long as your query doesn't grow too large for your DB (that is,
too many containers that might be considered)

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/CAD4ANxVC4SJjWM9_bw4uN3--HgcVDSj8s%3DAd%3D1vimTcxJ0Vh5A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to