Django的NotImplementedError:未实现annotate()+ distinct(fields)

时间:2016-07-26 07:11:43

标签: python django postgresql

有两个简单的模型:

class Question(TimeStampedModel):
    text = models.CharField(max_length=40)

class Answer(TimeStampedModel):
    question = models.ForeignKey(Question, related_name='answers')
    is_agreed = models.BooleanField()
    author = models.ForeingKey(User, related_name='answers')

现在,我将描述我的问题。

In [18]: Question.objects.count()
Out[18]: 3

我需要使用'is_user_agreed'和'answers_amount'字段来注释查询集:

In [18]: user = User.objects.first() 
In [19]: qs = Question.objects.annotate(
    ...:             is_user_agreed=Case(
    ...:                 When(answers__in=user.answers.filter(is_agreed=True), then=Value(True)),
    ...:                 When(answers__in=user.answers.filter(is_agreed=False), then=Value(False)),
    ...:                 default=Value(None),
    ...:                 output_field=NullBooleanField(),
    ...:             ),
    ...:         ).annotate(answers_amount=Count('answers'))
    ...: qs.count()
Out[19]: 4 

^这里的数量是4,但我在db中只有3个问题 :( 所以,我尝试过distinct()

In [20]: qs.distinct().count()
Out[20]: 4  # but distinct doesn't work

In [21]: qs.distinct('id').count()

在最后一行代码之后我得到了这个例外:

NotImplementedError: annotate() + distinct(fields) is not implemented.

我也试过使用这个技巧annotate(Count('id')).filter(id__count__gt=1)

但是在这种情况下我丢失了所有重复的行,而qs.count()是2。

更新:问题是查询集中的重复行。

解决方案:(弗拉基米尔第二种方法的扩展变体)

user = User.objects.first()
user_agreed_questions = user.answers.filter(
    is_agreed=True).values_list('question_id', flat=True)

user_not_agreed_questions = user.answers.filter(
    is_agreed=False).values_list('question_id', flat=True)

Question.objects.annotate(
    answer_amount=Count('answers'),
    is_user_agreed=Case(
         When(id__in=user_agreed_questions, then=True),
         When(id__in=user_not_agreed_questions, then=False),
         default=None,
         output_field=NullBooleanField(),
    ),
)

1 个答案:

答案 0 :(得分:8)

试试这个:

Question.objects.annotate(
    answer_amount=Count('answers'),
    is_user_agreed=F('answers__is_agreed'),
).order_by('id', '-answers__is_agreed').distinct('id')

如果question没有answers,则question.is_user_agreedNone。如果question至少有一个answeranswer.is_agreed=True,则question.is_agreedTrue。否则,is_user_agreedFalse

或者这个:

agreed_questons = Answer.objects.filter(
    is_agreed=True,
).values_list('question_id', flat=True).distinct()

Question.objects.annotate(
    answer_amount=Count('answers'),
    is_agreed=Case(
        When(id__in=agreed_questions, then=True),
        When(answers__isnull=True, then=None),
        default=False,
        output_field=NullBooleanField(),
    ),
)

agreed_questonsid questions的列表,其answer至少有一个answer.is_agreed=True