以下是我在模型中的一種保存方法。django自定義保存方法問題與新創建的對象ID
我試圖使用新創建的id當對象被保存到填充另一個字段,如第73行所示。這可能不會很好,因爲該對象尚未創建。
做什麼,我試圖達到什麼最好的方式?我應該將超級方法移到哪裏?
64 def save(self, *args, **kwargs):
65 '''
66 Save invoice and receipt number
67 '''
68
69 if self.status == 'pending' and self.invoice is None:
71 invoice = "%s-Inv-%s-%s" % (self.event.event_acronym,
72 date.today().strftime('%y%m%d'),
73 self.id)
74 self.invoice = invoice
75 super(Order, self).save(*args, **kwargs)
76
77 if self.status == 'completed' and self.receipt is None:
78 total_success = Order.objects.all.filter(status='completed').count()
79 if not total_success:
80 receipt = "%s-R-%s-%s" % (self.event.acronym,
81 date.today().strftime('%y%m%d'),
82 1)
83 else:
84 receipt = "%s-R-%s-%s" % (self.event.event_acronym,
85 date.today().strftime('%y%m%d'),
86 total_success + 1)
87 self.receipt = receipt
88 self.receipt_date = datetime.datetime.now
89 super(Order, self).save(*args, **kwargs)