gpt4 book ai didi

Django,鹡鸰管理员 : handle many to many with `through`

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

我有 2 个带有 through 表的模型,例如:

class A(models.Model):
title = models.CharField(max_length=500)
bs = models.ManyToManyField(to='app.B', through='app.AB', blank=True)

content_panels = [
FieldPanel('title'),
FieldPanel('fields'), # what should go here?
]

class AB(models.Model):
a = models.ForeignKey(to='app.A')
b = models.ForeignKey(to='app.B')
position = models.IntegerField()

class Meta:
unique_together = ['a', 'b']

我在尝试保存时遇到以下错误:

Cannot set values on a ManyToManyField which specifies an intermediary model.

这个错误对我来说很有意义。我应该保存 AB 实例。我只是不确定在 Wagtail 中实现该目标的最佳方法是什么。

最佳答案

您需要的是一个 InlinePanel:http://docs.wagtail.io/en/v2.0.1/getting_started/tutorial.html#images

from wagtail.admin.edit_handlers import FieldPanel, InlinePanel
from wagtail.core.models import Orderable
from modelcluster.fields import ParentalKey

# the parent object must inherit from ClusterableModel to allow parental keys;
# this happens automatically for Page models
from modelcluster.models import ClusterableModel

class A(ClusterableModel):
title = models.CharField(max_length=500)

content_panels = [
FieldPanel('title'),
InlinePanel('ab_objects'),
]

class AB(Orderable):
a = ParentalKey('app.A', related_name='ab_objects')
b = models.ForeignKey('app.B')
panels = [
FieldPanel('b'),
]

关于Django,鹡鸰管理员 : handle many to many with `through` ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50209851/

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