Filtering only on Annotations in Django

后端 未结 6 2037
执念已碎
执念已碎 2020-12-13 14:13

Taking the example from: http://docs.djangoproject.com/en/dev/topics/db/aggregation/#filter-and-exclude

Publisher.objects.filter(book__rating__gt=3.0).annota         


        
相关标签:
6条回答
  • 2020-12-13 14:37

    You can use the annotation variable in the filter.

    publishers=Publisher.objects.annotate(num_books=Count('book')).filter(num_books__gte=2)
    
    0 讨论(0)
  • 2020-12-13 14:45

    Hm, I think you have to use an extra clause:

    Publisher.objects.extra(select={
        'num_books': 'SELECT COUNT(*) ' + \
                     'FROM <your_app>_book ' + \
                     'WHERE <your_app>_book.publisher_id = ' + \
                           '<your_app>_publisher.id AND ' + \
                           'rating > 3.0'
    })
    
    0 讨论(0)
  • 2020-12-13 14:45
    from django.db import models
    
    Publisher.objects.annotate(
        num_books=models.Sum(
            models.Case(
                models.When(
                    book__rating__gt=3.0,
                    then=1,
                ),
                default=0,
                output_field=models.IntegerField(),
            )
        )
    ).filter(
        num_books=0,
    )
    
    0 讨论(0)
  • 2020-12-13 14:50

    I just facing this kind of problem. And if my interpreation for the problem and expected solution is correct, this is my working solution:
    Publisher.objects.annotate(num_books=Count('book')).filter(book__rating__gt=3.0) Just swap filter & annotate position. This is done in Django version 1.9

    0 讨论(0)
  • 2020-12-13 14:51

    You can try something like:

    Book.objects.values('publisher').annotate(num_books=Count('id'))
    
    0 讨论(0)
  • 2020-12-13 14:55

    Starting Django 2.0 one may use filter parameter of aggregation function:

    from django.db.models import Q    
    Publisher.objects.annotate(num_books=Count('book', filter=Q(book__rating__gt=3.0)))
    

    The answer is based on a cheat sheet.

    0 讨论(0)
提交回复
热议问题