gpt4 book ai didi

python - 将 InlineField 添加到 ClusterableModel 不起作用

转载 作者:行者123 更新时间:2023-12-01 07:31:03 25 4
gpt4 key购买 nike

我有一个名为 EventDetail 的 Page 类,它又保存多条消息(通过 cronjob 发送出去)。这是按照此处的文档所示完成的: https://docs.wagtail.io/en/v2.5.1/reference/pages/panels.html?highlight=available%20panel%20types#inline-panels

问题是我还希望能够将多个文件附加到每条消息中。但现在我不再参加 Page 类(class)了,我无法让它工作。我在其他论坛帖子上读到,可以通过将 InlinePanel 保存为 ClusterableModel 的模型来实现这一点,但我无法让它工作。执行此操作时,我在访问页面时收到以下信息:

FieldError at /admin/message/eventdetailmessage/create/
Unknown field(s) (files) specified for EventDetailMessage

我的代码:

class Message(models.Model):
"""
An abstract model of messages connected to an eventdetail
"""

title = models.CharField(max_length=255, blank=True)
content = models.TextField()
send_time = models.DateTimeField(verbose_name=_("Message send time"))
sent = models.BooleanField(default=False)

@property
def edit_link(self):
return self.url_helper.get_action_url("edit", self.id)

@property
def delete_link(self):
return self.url_helper.get_action_url("delete", self.id)

@property
def create_link(self):
return f"{self.url_helper.get_action_url('create')}?id={self.event_detail.id}"

panels = [FieldPanel("title"), FieldPanel("content"), FieldPanel("send_time")]

class Meta:
abstract = True

class EventDetailMessage(ClusterableModel, Orderable, Message):
"""
The actual implementation. Done like this in case we want do use
wagtails built in functionality for creating on the fly.
"""

event_detail = ParentalKey(
EventDetail, on_delete=models.CASCADE, related_name="messages"
)

panels = [FieldPanel("event_detail"), InlinePanel("files")] + Message.panels

def __init__(self, *args, **kwargs):
from .wagtail_hooks import MessageDataAdmin

self.url_helper = MessageDataAdmin().url_helper
super().__init__(*args, **kwargs)

def __str__(self):
return self.title


class File(models.Model):
file = models.FileField()
message = ParentalKey(
EventDetailMessage, on_delete=models.CASCADE, related_name="files"
)

panels = [FieldPanel("file")]

最佳答案

我找到了! File类下的ParentalKey定义需要用引号定义,否则不起作用,应该是这样的:

message = ParentalKey(
"message.EventDetailMessage", on_delete=models.CASCADE, related_name="files"
)

但是我仍然不确定这是为什么,有人可以解释一下吗?

关于python - 将 InlineField 添加到 ClusterableModel 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57197463/

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