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))
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.
On Sunday, November 9, 2014 1:01:42 AM UTC-7, Shai Berger wrote:
>
> Hello George,
>
> On Sunday 09 November 2014 05:33:36 George Ma wrote:
> > I've encountered quite a few cases where I need to write complicated
> > queries where a exists subquery makes more sense and has better
> > performance. So far, there's no exists subquey support in queryset api.
> > Please add it.
>
> I assume the "exists()" method on querysets does not give you what you
> want,
> but it isn't quite clear from the above what that is.
>
> Could you please give an example of the API you'd like to see and the SQL
> that
> would be generated by it?
>
> Thanks,
> Shai.
>
--
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/20dcdf81-c887-4472-861a-3f6fa2d28c46%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.