Django,Wagtail管理员:通过'through'处理多对多`
我有两个型号,带有至表格,例如:Django,Wagtail管理员:通过'through'处理多对多`,django,wagtail,Django,Wagtail,我有两个型号,带有至表格,例如: 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'), # wha
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']
尝试保存时出现以下错误:
无法在指定中间模型的ManyToManyField上设置值
这个错误对我来说是有道理的。我应该保存AB实例。我只是不确定在Wagtail中实现这一点的最佳方法是什么。您需要的是一个内嵌面板:
我尝试了
InlinePanel
,结果出现了一个关键异常。我缺少的是使用ParentalKey
&ClusterableModel
。我认为上面的ParentalKey应该没有“models”
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'),
]