Django相关模型的一种形式

时间:2019-02-27 20:27:44

标签: python-3.x django-models django-forms django-2.1

如何创建一个表单来处理两个以上相关模型?我可以对两个模型使用内联表单集,但不能完全弄清楚如何在表单中获得多个。例如:

class Project(models.Model):
    project_number = models.CharField(
        'Project Number',
        max_length=10,
    )
    ...

class Permit(models.Model):
    projectNumber = models.ForeignKey(
        Project,
        on_delete=models.SET_NULL,
        related_name='permit_projectNumber'
    )
    permitNumber = models.CharField(
        'Permit Number',
        max_length=10,
    )
    ...

class Utility(models.Model):
    projectNumber = models.ForeignKey(
        Project,
        on_delete=models.SET_NULL,
        related_name='utility_projectNumber',
    )
    permitNumber = models.ForeignKey(
        Permit,
        on_delete=models.SET_NULL,
        related_name='utility_permitNumber',
    )
    ...

class Checklist(models.Model):
    projectNumber = models.ForeignKey(
        Project,
        on_delete=models.SET_NULL,
        related_name='checklist_projectNumber',
    )
    permitNumber = models.ForeignKey(
        Permit,
        on_delete=models.SET_NULL,
        related_name='checklist_permitNumber',
    )
    ...

您将如何为所有这些模型创建一个表单,并在保存表单时填充相关字段?

0 个答案:

没有答案