1
의 두 모델 이 있습니다. 첫 번째 인스턴스를 저장할 때이 모델의 필드 값을 다른 필드로 보내야합니다.raise_exception을 사용하지 않고 pre_save 신호에 인스턴스를 저장하지 마십시오.
첫 번째 모델 :
class ModelOne(models.Model):
# fields...
quantity = models.FloatField()
두 번째 모델 :
class ModelTwo(models.Model):
# fields...
quantity = models.FloatField()
pre_save 신호 :
@receiver(pre_save, sender=ModelOne)
def verify(sender, instance, **kwargs):
# Stuff
quantity = instance.quantity
founded_model_two = ModelTwo.objects.get("""Something""")
future_result = founded_model_two.quantity - quantity
if future_result < 0:
raise Exception("Cannot be less than zero")
내가 인스턴스를 저장하지 않도록하려면,하지만 난 싶지 않아 예외를 제기하기 위해