我正在使用django-mptt包来开发我的评论应用程序,以下是我的模型:
class Comment(MPTTModel):
content = models.TextField(verbose_name='Treść')
author = models.ForeignKey(AUTH_USER_MODEL, verbose_name='Autor', blank=False, null=True)
is_deleted = models.BooleanField(verbose_name='Komentarz usunięty', default=False,
help_text='Zaznacz, aby usunąć komentarz')
ip = models.GenericIPAddressField(default=0, verbose_name='Adres IP')
content_type = models.ForeignKey(ContentType, verbose_name='Typ obiektu')
object_id = models.PositiveIntegerField(verbose_name='ID obiektu')
content_object = GenericForeignKey('content_type', 'object_id')
parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True)
hotness = models.FloatField(default=0)
created_at = models.DateTimeField(auto_now_add=False, verbose_name='Data dodania')
updated_at = models.DateTimeField(auto_now=True, verbose_name='Aktualizacja')
class MPTTMeta:
order_insertion_by = ('-hotness', '-created_at')
class Meta:
verbose_name = 'Komentarz'
verbose_name_plural = 'Komentarze'
def __unicode__(self):
if len(self.content) > 50:
return self.content[:50] + '...'
else:
return self.content
我希望为用户提供按热度或创建日期排序评论树的可能性。是否可以从视图编辑order_insertion_by
字段以生成两种类型的排序(按日期、按热度)?感谢您的帮助。