我有 2 个模型 Transaction
和 FamilyGroup
,我正在做一些如下的验证
class Transaction(models.Model):
chp_reference = models.CharField(max_length=50, unique=True)
rent_effective_date = models.DateField(null=True, blank=True)
income_period = models.CharField(max_length=11)
property_market_rent = models.DecimalField(max_digits=7)
number_of_familygroup = models.DecilmalField(max_digits=10)
def clean(self):
count_fg = self.family_groups.count()
if self.number_of_family_group != count_fg:
raise ValidationError(
' Number of family group doesnt match with FG_NO.')
class FamilyGroup(models.Model):
name = models.CharField(max_length=10)
transaction = models.ForeignKey(Transaction,
on_delete=models.CASCADE,related_name='family_groups')
last_rent = models.DecimalField(max_digits=7)
问题是当我将 number_of_familygroup
更改为例如。 3 并且我创建了 3 个 FamilyGroup 对象,它仍然给我一个错误 Number of family group doesnt match with FG_NO
,它不允许我保存新值。使其正常工作的最佳方法是什么?提前致谢!