在 Django 的 log/history table 中插入记录的最佳方法是什么
What is the best way to insert a record in log/history table in django
I have two models:
class RateCard(models.Model):
name = models.CharField()
pricing_type = models.ForeignKey(PriceAttribute)
and
class RateCardHistory(models.Model):
name = models.CharField()
pricing_type = models.ForeignKey(PriceAttribute)
我想要的是在 RateCard 中的每个操作上都应该有一条新记录插入 RateCardHistory
哪一个是实现此 Django 信号的最佳方法 post_save 或超越 RateCard 保存方法,或者是否有任何其他好的方法。
您可以覆盖 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')
I have two models:
class RateCard(models.Model):
name = models.CharField()
pricing_type = models.ForeignKey(PriceAttribute)
and
class RateCardHistory(models.Model):
name = models.CharField()
pricing_type = models.ForeignKey(PriceAttribute)
我想要的是在 RateCard 中的每个操作上都应该有一条新记录插入 RateCardHistory
哪一个是实现此 Django 信号的最佳方法 post_save 或超越 RateCard 保存方法,或者是否有任何其他好的方法。
您可以覆盖 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')