0

我有 2 个模型:报表STO,它们之间是一对多的关系,所以一个 STO 可以有很多相关的报表。

STO模型

class STO(TerminableWithTerminableRelatedModel):
    name = models.CharField(_('name'), unique=True, blank=False, max_length=255)
    description = models.TextField(_('description'), blank=True, null=True)
    answer_no = models.PositiveSmallIntegerField(_('number of answers'), default=5) # that's the value I'm interested in
    result_note = models.TextField(_('STO result note'), blank=True, null=True)

报告模型

class Report(models.Model):

    @property
    def answer_no(self):
        return STO.objects.get(id=self.STO).answer_no

    date = models.DateField(_('date'), blank=False, null=False)
    STO = models.ForeignKey(STO, on_delete=models.CASCADE, related_name='reports')
    answers = ArrayField(AnswerField(), size=answer_no, blank=False, null=False) # here comes the problem

如您所见,每个 Report 都有一个AnswerField 数组AnswerField是一个自定义字段,我省略了它,因为它超出了本问题的范围)。该数组的大小应在相关 STO 类的answer_no字段上指定。我做出这个决定是因为这个值对于给定 STO 的每个报告都是相等的,因此在每个单独的报告中插入它会导致数据重复。我尝试使用property来实现这一点,但 Django 抱怨说他在迁移时无法序列化属性:

ValueError: Cannot serialize: <property object at 0x7faf02161ea0>
There are some values Django cannot serialize into migration files.
4

0 回答 0