Django models 在查询集集合上使用排序
我有这个页面模型:Django models 在查询集集合上使用排序,django-models,django-queryset,wagtail,Django Models,Django Queryset,Wagtail,我有这个页面模型: class BlogPostPage(Page): date_published= models.DateField('Published date', blank=True, null=True) intro= models.CharField(max_length=250) body = RichTextField(blank=True) header_image = models.ForeignKey('wagtailimages.Ima
class BlogPostPage(Page):
date_published= models.DateField('Published date', blank=True, null=True)
intro= models.CharField(max_length=250)
body = RichTextField(blank=True)
header_image = models.ForeignKey('wagtailimages.Image', blank=True, null=True,
on_delete=models.SET_NULL, related_name='+')
tags = ClusterTaggableManager(through='blog.BlogPageTag', blank=True)
categories = ParentalManyToManyField('blog.BlogCategory', blank=True)
def get_context(self, request):
context = super().get_context(request)
all_categories = []
for post in self.get_siblings(inclusive=True):
all_categories +=post.specific.categories.all()
all_categories = sorted(set(all_categories))
context['all_categories']= all_categories
return context
当页面被访问时,我得到一个类型错误:
在/blog/xxx处键入错误/
“您可以根据定义排序的
的键
参数
但是我不确定:all\u categories+=post.specific.categories.all()
?我想我发现了我的错误。我试图对BlogCategory对象进行排序,而排序方法不知道如何在哪个字段上进行排序。那么我该如何克服这一点呢?提前谢谢。