我创建了一个Planning
和一个Meeting
模型。我使用 WagtailModelAdmin
来管理它们。Planning
有一个planning_panels
哪个是一个InlinePanel
。
对于其他模型,我可以使用表单的方法设置初始数据__init__
。
但我不知道如何formsets
为InlinePanel
. 有没有人有任何想法?这是代码:
class Planning(ClusterableModel):
base_form_class = PlanningForm
planning_panels = [
InlinePanel(
'planning_meetings',
min_num = 2,
max_num = 8,
label = 'meetings'
)
)
edit_handler = TabbedInterface([
ObjectList(planning_panels, heading=_('meetings')),
])
class PlanningMeeting(models.Model):
planning = ParentalKey(
'cms.Planning',
related_name='planning_meetings',
)
start = models.DateTimeField(
'start'
)
finish = models.DateTimeField(
'finish'
)
panels = [
FieldPanel('start'),
FieldPanel('finish')
]
class Meta:
verbose_name = 'Planned meeting'
class PlanningForm(WagtailAdminModelForm):
class Meta:
fields = '__all__'
def __init__(self, *args, **kwargs):
instance = kwargs.get('instance')
if not instance or not instance.pk:
initial = kwargs.get('initial', {})
initial.update({
'some_fiel': 'some_value'
})
kwargs['initial'] = initial
super().__init__(*args, **kwargs)
class CreatePlanningView(CreateView):
pass
class PlanningAdmin(ModelAdmin):
model = Planning
create_view_class = CreatePlanningView