3

假装我有这些课程:

# For storing arbitrary blobs
class GenericAttachment(models.Model):
    # Some fields...
    type = models.PositiveSmallIntegerField("Type", choices=('Generic', 'Report', 'Image', 'Budget'))
    # Some methods...


class ReportAttachment(GenericAttachment):
    # Report specific fields
    pass

class BudgetAttachment(GenericAttachment):
    # Budget-specific fields
    pass

class Record(models.Model):
    attachments = models.ManyToManyField(GenericAttachment, blank=True, null=True)

class DataEntry(models.Model):
    attachments = models.ManyToManyField(GenericAttachment, blank=True, null=True)

真正的稍微复杂一些,但并不多。

当 A 的类型是“报告”时,我想将报告类用于额外字段,与预算相同。但是,其他类型都没有自己的独特字段,因此不需要自己的子类。我还想避免每个 Record/DataEntry 有 3 个 ManyToMany 字段...最好列出所有附件,但它应该根据实际类型打开正确的创建/编辑表单。

理想情况下,我想要一个界面,在用户更改类型时添加和删除报告和预算特定字段,并保存为正确的类型。我可以编写自己的视图来完成它,但是这个小应用程序的其余部分是通过管理界面完成的,我希望保持整个过程。所以......谁能想到在管理界面中获得这种行为的好方法?

4

0 回答 0