{{{
class Product(models.Model):
name = models.CharField(max_length=127)
category = models.ForeignKey('Category', on_delete=models.CASCADE,
related_name='products')
class Category(models.Model):
name = models.CharField(max_length=127)
parent = models.ForeignKey('self', on_delete=models.CASCADE,
null=True, blank=True, related_name='children')
}}}
I want to get all categories that have at least one product-related or
their children to have at least one product.
So I have a query like:
{{{
Category.objects.all().annotate(product_count_parent=Count('products')).filter(Q(product_count_parent__gt=0)
|
Q(children__id__in=Category.objects.filter(children=None).annotate(product_count=Count('products')).filter(product_count__gt=0)))
}}}
I have gotten this error
{{{
ProgrammingError: subquery must return only one column
LINE 1: ... GROUP BY "nakhll_market_category"."id", T3."id", (SELECT U0...
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33775>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* resolution: => invalid
Comment:
You are using an `__in=QuerySet` filter
[https://docs.djangoproject.com/en/4.0/ref/models/querysets/ without
limiting the columns returned by your inner query].
Please TicketClosingReasons/UseSupportChannels for further support.
--
Ticket URL: <https://code.djangoproject.com/ticket/33775#comment:1>