所以我有一个具有字段current_count
的PickupSchedule模型。捐赠提交后,current_count会增加。当捐赠被重新安排时,它会减少并且新的路线会增加。还有一个豁免程序可以做同样的事情。为了避免重复的代码我希望做类似下面的事情,但它不起作用(属性错误 - 'PickupSchedule' object has no attribute 'decrease_current_count'
)。我知道我可以+ =表单中的字段保存,但我想将它放在一个单独的位置,而不是它是两个单独的形式。
models.py
class PickupSchedule(models.Model):
current_count= models.IntegerField(blank=True, default=0)
def __unicode__(self):
return self.route
def decrease_current_count(self):
self.current_count -= 1
self.save()
def increase_current_count(self):
self.current_count += 1
self.save()
forms.py save
def save(self,force_insert=False, force_update=False,commit=True):
donor = super(RescheduleForm,self).save(commit=False)
# Update the old route current count
old_route = self.old_route
old_route.decrease_current_count()
# Update the new route current count
new_route = PickupSchedule.objects.get(pickup_id = self.new_id)
new_route.increase_current_count()
if commit:
donor.save()
return donor