【发布时间】:2020-05-15 19:56:19
【问题描述】:
我使用django-hitcont 来计算我的帖子模型的浏览量。我正在尝试使用此查询objects.order_by('hit_count_generic__hits') 在我的 ListView 中获得观看次数最多的帖子,它在 SQLite 上运行良好,但在 PostgreSQL 上,它给了我这个错误:
django.db.utils.ProgrammingError: operator does not exist: integer = text LINE 1: ...R JOIN "hitcount_hit_count" ON ("posts_post"."id" = "hitcoun....
models.py
class Post(models.Model, HitCountMixin):
author = models.ForeignKey(User, related_name='authors', on_delete=models.CASCADE)
title = models.CharField('Post Title', max_length = 150)
description = models.TextField('Description', max_length=1000, blank = True)
date_posted = models.DateTimeField('Date posted', default = timezone.now)
date_modifed = models.DateTimeField('Date last modified', default = timezone.now)
document = models.FileField('Document of Post', upload_to='documents', \
validators=[FileExtensionValidator(allowed_extensions = ['pdf', 'docx']), validate_document_size] \
)
hit_count_generic = GenericRelation(
HitCount,
object_id_field='object_pk',
related_query_name='hit_count_generic_relation'
)
views.py
queryset = Post.objects.order_by('hit_count_generic__hits')
我在 Github 上发现了与该问题相关的 this issue,但我仍然无法找出上述解决方法。
【问题讨论】:
-
能否提供 HitCountModified 模型?我猜连接语句上的字段类型不匹配。
-
@DenizKaplan 我的错,它只是 HitCount。在我的数据库中,object_pk 是文本类型,应该是整数。我需要的是一种将 HitCount object_pk 更改为整数的方法,但我不知道如何在我的项目中修改 HitCount 的字段。
标签: django postgresql github hitcounter