2015-07-28 190 views
0

我需要確保如果模型M中的attribute A具有某個值,那麼attribute B將不會是None根據另一個字段限制字段的值

例如:

M.A == True then M.B != None 

M.A = False then M.B = anything (None, int..) 

回答

1

您可以使用Model.clean()

class M(models.Model): 
    A = models.BooleanField() 
    B = models.IntegerField(null=True, blank=True) 

    def clean(self): 
     if self.A and self.B is None: 
      raise ValidationError("B can not be None lwhile A is None") 

您將提高ValidationError在無效的條件。

+0

thx。那解決了它。 – brad

相關問題