django-subquery

Django conditional Subquery aggregate

那年仲夏 提交于 2021-01-21 07:10:12
问题 An simplified example of my model structure would be class Corporation(models.Model): ... class Division(models.Model): corporation = models.ForeignKey(Corporation) class Department(models.Model): division = models.ForeignKey(Division) type = models.IntegerField() Now I want to display a table that display corporations where a column will contain the number of departments of a certain type, e.g. type=10 . Currently, this is implemented with a helper on the Corporation model that retrieves

Django conditional Subquery aggregate

余生颓废 提交于 2021-01-21 07:10:03
问题 An simplified example of my model structure would be class Corporation(models.Model): ... class Division(models.Model): corporation = models.ForeignKey(Corporation) class Department(models.Model): division = models.ForeignKey(Division) type = models.IntegerField() Now I want to display a table that display corporations where a column will contain the number of departments of a certain type, e.g. type=10 . Currently, this is implemented with a helper on the Corporation model that retrieves

Django Exists() / ~Exists() return if there is no matching data?

五迷三道 提交于 2020-03-25 18:56:13
问题 EDIT: As per schillingt's answer below I have switched to using Case/When: context['db_orders'] = Order.objects.filter( retailer_code=self.object.retailer_code).annotate( in_db=Case(When(Q(Subquery(self.object.suppliers.filter( supplier_code=(OuterRef('supplier_code'))) ), then=Value(True), default=Value(False), output_field=NullBooleanField())))) However I'm now struggling with an errror: FieldError at /retailers/A001/ Cannot resolve expression type, unknown output_field Original question: I

Django 1.11 Annotating a Subquery Aggregate

允我心安 提交于 2019-12-17 08:22:07
问题 This is a bleeding-edge feature that I'm currently skewered upon and quickly bleeding out. I want to annotate a subquery-aggregate onto an existing queryset. Doing this before 1.11 either meant custom SQL or hammering the database. Here's the documentation for this, and the example from it: from django.db.models import OuterRef, Subquery, Sum comments = Comment.objects.filter(post=OuterRef('pk')).values('post') total_comments = comments.annotate(total=Sum('length')).values('total') Post

Django subquery and annotations with OuterRef

筅森魡賤 提交于 2019-12-10 12:55:18
问题 I'm having problems using annotate() with OuterRef in Django 1.11 subqueries. Example models: class A(models.Model): name = models.CharField(max_length=50) class B(models.Model): a = models.ForeignKey(A) Now a query with a subquery (that does not really make any sense but illustrates my problem): A.objects.all().annotate( s=Subquery( B.objects.all().annotate( n=OuterRef('name') ).values('n')[:1], output_field=CharField() ) ) This gives the following error: Traceback (most recent call last):

Django 1.11 Annotating a Subquery Aggregate

巧了我就是萌 提交于 2019-11-27 03:12:25
This is a bleeding-edge feature that I'm currently skewered upon and quickly bleeding out. I want to annotate a subquery-aggregate onto an existing queryset. Doing this before 1.11 either meant custom SQL or hammering the database. Here's the documentation for this , and the example from it: from django.db.models import OuterRef, Subquery, Sum comments = Comment.objects.filter(post=OuterRef('pk')).values('post') total_comments = comments.annotate(total=Sum('length')).values('total') Post.objects.filter(length__gt=Subquery(total_comments)) They're annotating on the aggregate, which seems weird