3

我创建了一个Planning和一个Meeting模型。我使用 WagtailModelAdmin来管理它们。Planning有一个planning_panels哪个是一个InlinePanel

对于其他模型,我可以使用表单的方法设置初始数据__init__
但我不知道如何formsetsInlinePanel. 有没有人有任何想法?这是代码:

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
4

1 回答 1

1

鹡鸰InlinePanel是使用django-modelcluster包。考虑到这一点,您可以通过覆盖get_instance方法 fromCreateView然后添加planning_meetings列表来将初始数据添加到 wagtail create model admin 中。

这是代码:

class CreatePlanningView(CreateView):
    def get_instance(self):
      instance = super().get_instance()

      # add the initial inline panels here
      instance.planning_meetings = [
        PlanningMeeting(start=start, end=end),
        PlanningMeeting(start=start, end=end)
        # ... add more initial datas
      ]

      # dont forget to return the instance
      return instance

提示来自django-modelcluster文档https://github.com/wagtail/django-modelcluster

于 2019-11-17T01:51:59.537 回答