gpt4 book ai didi

wagtail - 预览 Wagtail 页面和获取相关内联时出错

转载 作者:行者123 更新时间:2023-12-02 00:24:07 27 4
gpt4 key购买 nike

我在预览 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() 来获取作者列表。如果文章是“实时”的,或者如果我在 shell 中的对象上调用相同的方法,这就可以正常工作,但是在预览页面时我得到这个:

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 有什么奇怪的地方?

最佳答案

是的,这是 django-modelcluster 模块的限制。为了让 Django 查询集方法(例如 order_by)处理与真实数据库状态不匹配的内存关系(预览时就是这种情况,以及其他一些情况,例如查看旧修订版),modelcluster 必须“伪造”通常通过 SQL 查询完成的操作。 “伪造”的效果有一些限制,并且某些操作(例如原始 SQL 查询)实际上永远不可能实现。

通过外键缺少对 order_by 的支持是一个已知限制:https://github.com/wagtail/django-modelcluster/issues/45

在解决此问题之前,解决方法是将查询包围在 try/except AttributeError block 中,然后退回到无序列表。

关于wagtail - 预览 Wagtail 页面和获取相关内联时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54326493/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com