如何在wagtail的一个modeladmin表单中更新多个数据库表?

问题描述

在我的问题中,有三个相关模型:

class DicSoftware(index.Indexed,ClusterableModel):
    name = models.CharField("软件名称",max_length=255,blank=True)
    version = models.CharField("版本号",blank=True)

    panels = [MultiFieldPanel([
            FieldPanel('name',classname="col10"),FieldPanel('version',],"模拟软件")]


class Simulation(index.Indexed,ClusterableModel):
    name = models.CharField("算例名称",blank=True)
    software = ParentalManyToManyField('DicSoftware',related_name='模拟软件')
    panels = [ MultiFieldPanel([
            FieldPanel('name',FieldPanel('software',"算例")]

有了上面这两个模型,wagtail自动生成了包含id、simulation_id、dicsoftware_id三个字段的表simulation_software。但是,我想在表 Simulation_software 中添加另外两个字段,inputFile 和 inputFilePath。决赛桌模拟_软件的模型应该是:

class SimulationSoftware(index.Indexed,ClusterableModel):
    simulation = models.ForeignKey(Simulation,verbose_name="算例",help_text="/admin/home/simulation/",on_delete=models.CASCADE,blank=True,null=True,related_name='+')
    software = models.ForeignKey(DicSoftware,verbose_name="模拟软件",help_text="/admin/home/dicsoftware/",related_name='+')
    #把读入的文件内容存入inputJSON
    inputFile = models.FileField("初始化参数文件",upload_to="files",default="")
    outputFilePath = models.CharField("结果文件存储位置",blank=True)
    panels = [MultiFieldPanel([
            FieldPanel('simulation',FieldPanel('inputFile',FieldPanel('outputFilePath',"算例输入")]

用户添加一个模拟时,他们必须提供如下信息:

  1. 模拟名称
  2. 指定用于此模拟的软件(一个或多个软件)。

在一次模拟中,可以有一个或多个软件。如果本次模拟使用两个软件,用户应同时提供这些信息

  1. 一个软件的 inputFile 和 outputFilePath。
  2. 第二个软件的 inputFile 和 outputFilePath。

如何管理模型的结构以及软件(一个或多个软件)的 inputFiles 和 outputFilePaths 的输入面板。

谁能给我一些建议?希望得到您的帮助。非常感谢!

解决方法

您可以通过添加这样的属性(页面)来关联子模型:

page = ParentalKey('SimulationSoftware',related_name='<sub-class>_model',on_delete=models.CASCADE)

在每个子类中(DicSoftware 和 Simulation) 然后在主要可聚类模型的 content_panels 中,使用每个子类的 related_name 属性:FieldPanel("<sub-class>_model",classname="col10"),