小编San*_*pal的帖子

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

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

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) …
Run Code Online (Sandbox Code Playgroud)

django django-orm

5
推荐指数
1
解决办法
1611
查看次数

Django-Ecommerce 2013

我是一个django新手.我正在寻找一个框架来开发一个简单的电子商务网站.我已经回顾了其他帖子,但所有的问题和答案看起来有点旧.请建议一个简单易用且可靠的好框架.这是一个很好的django-shop吗?

django

3
推荐指数
1
解决办法
5386
查看次数

标签 统计

django ×2

django-orm ×1