在同一模型上应用 union() 无法识别使用 GenericRelation 的排序

San*_*pal 5 django django-orm

我有一个像这样的文章模型

from django.contrib.contenttypes.fields import GenericRelation
from django.db import models
from hitcount.models import HitCountMixin, HitCount

class Article(models.Model):
    title = models.CharField(max_length=250)
    hit_count_generic = GenericRelation(
    HitCount, object_id_field='object_pk',
    related_query_name='hit_count_generic_relation')
Run Code Online (Sandbox Code Playgroud)

当我这样做时Article.objects.order_by('hit_count_generic__hits'),我会得到结果。但是当我这样做时

articles_by_id = Article.objects.filter(id__in=ids).annotate(qs_order=models.Value(0, models.IntegerField()))
articles_by_name = Article.objects.filter(title__icontains='sports').annotate(qs_order=models.Value(1, models.IntegerField()))
articles = articles_by_id.union(articles_by_name).order_by('qs_order', 'hit_count_generic__hits')
Run Code Online (Sandbox Code Playgroud)

出现错误

ORDER BY 术语与结果集中的任何列都不匹配

我怎样才能实现这样的结合?我必须使用 union 而不是 AND 和 OR,因为我需要保留顺序。IE; articles_by_id 应该排在第一位,articles_by_name 应该排在第二位。

使用 Django hitcount 进行 hitcount https://github.com/thornomad/django-hitcount。点击数模型如下。

class HitCount(models.Model):
"""
Model that stores the hit totals for any content object.
"""
hits = models.PositiveIntegerField(default=0)
modified = models.DateTimeField(auto_now=True)
content_type = models.ForeignKey(
    ContentType, related_name="content_type_set_for_%(class)s", on_delete=models.CASCADE)
object_pk = models.TextField('object ID')
content_object = GenericForeignKey('content_type', 'object_pk')

objects = HitCountManager()
Run Code Online (Sandbox Code Playgroud)

正如 @Angela 所建议的,尝试了与预取相关的操作。

articles_by_id = Article.objects.prefetch_related('hit_count_generic').filter(id__in=[1, 2, 3]).annotate(qs_order=models.Value(0, models.IntegerField()))
articles_by_name = Article.objects.prefetch_related('hit_count_generic').filter(title__icontains='date').annotate(qs_order=models.Value(1, models.IntegerField()))
Run Code Online (Sandbox Code Playgroud)

选中时 prefetch_lated 的查询根本不选择命中计数。

 SELECT "articles_article"."id", "articles_article"."created", "articles_article"."last_changed_date", "articles_article"."title", "articles_article"."title_en", "articles_article"."slug", "articles_article"."status", "articles_article"."number_of_comments", "articles_article"."number_of_likes", "articles_article"."publish_date", "articles_article"."short_description", "articles_article"."description", "articles_article"."cover_image", "articles_article"."page_title", "articles_article"."category_id", "articles_article"."author_id", "articles_article"."creator_id", "articles_article"."article_type", 0 AS "qs_order" FROM "articles_article" WHERE "articles_article"."id" IN (1, 2, 3)
Run Code Online (Sandbox Code Playgroud)

Dip*_*iya 0

来自Django的官方文档:

\n\n
\n

此外,数据库对组合查询中允许的操作进行了限制。例如,大多数数据库不允许在组合查询中使用 LIMIT 或 OFFSET。

\n
\n\n

因此,请确保您的数据库允许组合这样的查询。

\n