你有幾種選擇。
第一個是重寫保存方法:
#Delegate
def save(self, *args, **kwargs):
if self.pk:
previous_excursion = Delegate.objects.get(self.pk).excursion
super(Model, self).save(*args, **kwargs)
if self.pk and self.excursion != previous_excursion:
#change booking
第二個是張貼節省信號+ django model utils field tracker結合功能:
@receiver(post_save, sender=Delegate)
def create_change_booking(sender,instance, signal, created, **kwargs):
if created:
previous_excursion = get it from django model utils field tracker
#change booking
而另一種解決方案是在pre_save正如你正在運行:
@receiver(pre_save, sender=Delegate)
def my_callback(sender, instance, *args, **kwargs):
previous_excursion = Delegate.objects.get(self.pk).excursion
if instance.pk and instance.excursion != previous_excursion:
#change booking
如果這是我的應用:[django model utils field tracker](https://django-model-utils.readthedocs.org/en/latest/utilities.html#accessing-a-field-tracker)+ [post_save信號](https://docs.djangoproject.com/en/dev/ref/signals/#post-save) – danihp 2014-09-03 15:12:02
這很酷!謝謝,讓我試試 – Harry 2014-09-03 15:13:56
檢查它。然後讓我知道它是否是您的解決方案,以便將其作爲答案發布。祝你好運。 – danihp 2014-09-03 15:15:27