且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将django默认模型字段值作为父属性值

更新时间:2023-01-30 11:34:41

我认为这可以通过覆盖modB的保存方法。

I think this could be done by overriding the save method of modB.

class modB(models.Model): 
    moda        = models.ForeignKey(modA)
    localField  = models.CharField(max_length=9,default=moda.asDefault)

    def save(self, *args, **kwargs):
        if self.pk is None:
            if self.moda and not self.localField:
                self.localField = self.moda.wantedField
        super(modB, self).save(*args, **kwargs)