预览Wagtail页面时出现错误,但是在发布和实时查看时很好。我的设置是这样的:
from django.db import models
from modelcluster.fields import ParentalKey
from wagtail.core.models import Orderable, Page
from wagtail.snippets.models import register_snippet
@register_snippet
class Author(models.Model):
name = models.CharField(max_length=255, blank=False)
class ArticleAuthorRelationship(Orderable, models.Model):
author = models.ForeignKey('Author',
on_delete=models.CASCADE,
related_name='articles')
page = ParentalKey('ArticlePage',
on_delete=models.CASCADE,
related_name='authors')
class ArticlePage(Page):
def get_authors(self):
"""Returns a list of Author objects associated with this article."""
return [a.author for a in self.authors.all().order_by('author__name')]
在ArticlePage
的模板中,我叫self.get_authors()
以获取作者列表。如果文章是“实时”的,或者我在外壳中的对象上调用了相同的方法,则此方法效果很好,但是在预览页面时,我得到了这一点:
File "/Users/phil/Projects/myproject/myapp/articles/models/pages.py", line 551, in get_authors
return [a.author for a in self.authors.all().order_by('author__name')]
File "/Users/phil/.local/share/virtualenvs/myproject-zPWVWoxf/lib/python3.6/site-packages/modelcluster/queryset.py", line 467, in order_by
sort_by_fields(results, fields)
File "/Users/phil/.local/share/virtualenvs/myproject-zPWVWoxf/lib/python3.6/site-packages/modelcluster/utils.py", line 19, in sort_by_fields
items.sort(key=lambda x: (getattr(x, key) is not None, getattr(x, key)), reverse=reverse)
File "/Users/phil/.local/share/virtualenvs/myproject-zPWVWoxf/lib/python3.6/site-packages/modelcluster/utils.py", line 19, in <lambda>
items.sort(key=lambda x: (getattr(x, key) is not None, getattr(x, key)), reverse=reverse)
AttributeError: 'ArticleAuthorRelationship' object has no attribute 'author__name'
我很困惑-我不明白预览Wagtail页面与正常查看相比有何不同。 modelcluster有点奇怪吗?
答案 0 :(得分:3)
是的,这是django-modelcluster
模块的限制。为了使order_by
之类的Django queryset方法能够处理与实际数据库状态不匹配的内存中关系(预览时就是这种情况,以及其他一些情况,例如查看旧修订版) ,modelcluster必须“伪造”通常通过SQL查询完成的操作。 “伪造”的工作方式存在一些限制,并且实际上将永远不可能进行某些操作(例如原始SQL查询)。
缺少通过外键支持order_by
的已知限制:https://github.com/wagtail/django-modelcluster/issues/45
在解决此问题之前,一种解决方法是将查询括在try
/ except AttributeError
块中,然后退回到无序列表。