预览 Wagtail 页面并获取相关内联时出错 [英] Error when previewing Wagtail page and getting related inlines

查看:25
本文介绍了预览 Wagtail 页面并获取相关内联时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在预览 Wagtail 页面时遇到错误,但在发布和实时查看时它们没问题.我的设置是这样的:

I'm getting errors when previewing Wagtail pages, but they're fine when published and viewed live. My set-up is something like this:

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 中的对象上调用相同的方法,这可以正常工作,但是在预览页面时,我得到了这个:

In a template for an ArticlePage I call self.get_authors() to get a list of authors. This works fine if the article is 'live', or if I call the same method on the object in the shell, but when previewing the page I get this:

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 页面与正常查看有什么不同.模型集群中有些奇怪的东西?

I'm stumped - I don't understand what's different about previewing a Wagtail page compared to viewing it normally. Something odd in modelcluster?

推荐答案

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

Yes, this is a limitation of the django-modelcluster module. In order for Django queryset methods such as order_by to work on in-memory relations that don't match the real database state (which is the case when previewing, as well as a few other situations such as viewing old revisions), modelcluster has to "fake" the operations that would normally be done through a SQL query. There are some limitations to how well the "faking" works, and some operations (such as raw SQL queries) will realistically never be possible.

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

The lack of support for order_by through a foreign key is a known limitation: https://github.com/wagtail/django-modelcluster/issues/45

在此问题得到解决之前,一种解决方法是将查询括在 try/except AttributeError 块中并退回到无序列表.

Until this is fixed, a workaround would be to surround the query in a try/except AttributeError block and fall back on the unordered list.

这篇关于预览 Wagtail 页面并获取相关内联时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆