Yes, the actual sql is very close to this one. But I just feel this approach is like a hack. Because I always have to reverse the logic first and then reverse again. Not as natural as exists subquery.
On Monday, November 10, 2014 5:53:47 PM UTC-7, Curtis Maloney wrote: > > If you want to know the SQL generated for any particular queryset, you can > just: > > print str(qs.query) > > On 11 November 2014 11:39, George Ma <[email protected] <javascript:>> > wrote: > >> For simplicity, let's assume we have a model A and model B. >> >> class A(models.Model): >> name = models.CharField(max_length=200) >> >> class B(models.Model): >> a = models.ForeignKey(A) >> criteria = models.BooleanField(default = False) >> >> Let's say there's a requirement to find A that doesn't have a B with a >> true criteria. To me, it makes more sense to use a "not exists" subquery. >> Right now, I have to find all the A with the reverse conditions and exclude >> such cases. Something like: >> >> A.objects.exclude(pk__in = A.objects.filter(b__criteria = True)) >> > > I think you'll find this would be something like: > > SELECT .... FROM myapp_a WHERE NOT pk IN (SELECT id FROM myapp_a T1 INNER > JOIN myapp_b T2 ON (T1.id = T2.a_id) WHERE T2.criteria = True) > > The problem with this approach is that: first I don't know how the >> performance would be; second, it's not very natural from sql query's >> perspective. >> > > Performance should be ok, from what I can see... > > Would be interested to see what the actual SQL is. > > -- > Curtis > > -- You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" 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]. 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/19f716f4-2367-492c-ac1e-a4f48c4a2c01%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
