utils.py
我想要的是在RateCard的每个操作中,新记录应该插入RateCardHistory
哪一个最好的方法来实现这个Django信号post_save或过度使用RateCard保存方法,还是有任何其他方法会很好。
答案 0 :(得分:0)
您可以在RateCard
class RateCard(models.Model):
name = models.CharField()
pricing_type = models.ForeignKey(PriceAttribute)
def save(self, *args, **kwargs):
# the call to the super save the record as usual
super(RateCard,self).save(*args,**kwargs)
# do here what you want... create your new related records
new_card_history = RateCardHistory.objects.create(name='the name', pricing_type=self)
我建议你对代码进行一些更改。如果要按创建顺序获取字段,请添加创建日期时间,并添加相关名称以从RateCard
class RateCardHistory(models.Model):
name = models.CharField()
created = models.DateTimeField(auto_now_add=True)
pricing_type = models.ForeignKey(PriceAttribute, related_name='histories')