django:如何为嵌套模型节省价值

时间:2012-10-16 07:31:55

标签: python django django-models

我的模型具有嵌套的多对多关系

IndependentTest 协议有多对多关系

协议格式有多对多的关系。

所以我需要针对 IndependentTests 为特定用户保存测试结果

编辑:以下是模型

class IndependentTest(models.Model):
    name = models.CharField(max_length=1000)
    choice = models.ManyToManyField(to=TestChoice, null=True, blank=True)
    required = models.BooleanField()
    testType = models.CharField(max_length=200,
                                choices=Values.CUSTOM_ATTRIB_TYPES)

class TestProtocol(models.Model):
    name = models.CharField(max_length=200)
    description = models.CharField(max_length=1000)
    independent_test = models.ManyToManyField(IndependentTest,
                                              through='PIOrder')

class PIOrder(models.Model):
    protocol = models.ForeignKey(TestProtocol)
    independent = models.ForeignKey(IndependentTest)
    order = models.IntegerField()

class TestFormat(models.Model):
    name = models.CharField(max_length = 100)
    test_protocol = models.ManyToManyField(TestProtocol, through='FPOrder')

class FPOrder(models.Model):
    format = models.ForeignKey(TestFormat)
    protocol = models.ForeignKey(TestProtocol)
    order = models.IntegerField()

编辑:我不知道这是不是一个好方法,但我可以创建一个新类

class CandidateTest (models.Model):
    test_value  = models.CharField(max_length = 200)
    candidate   = models.ForeignKey(Candidate)
    test        = models.ForeignKey(IndependentTest)
    testProtocol = models.ForeignKey(TestProtocol)
    testFormat  = models.ForeignKey(TestFormat)

虽然结果会保存在test_value中,但是测试的日期或地点等其他任何数据呢?

0 个答案:

没有答案