python - Django:如何在 post_save 信号中访问原始(未修改)实例

标签 python django postgresql denormalization django-signals

我想进行数据非规范化以获得更好的性能,并将我的博客文章收到的投票总和放入 Post 模型中:

class Post(models.Model):
    """ Blog entry """
    author          = models.ForeignKey(User)
    title           = models.CharField(max_length=255)
    text            = models.TextField()
    rating          = models.IntegerField(default=0) # here is the sum of votes!

class Vote(models.Model):
    """ Vote for blog entry """
    post            = models.ForeignKey(Post)
    voter           = models.ForeignKey(User)
    value           = models.IntegerField()

当然,我需要保持 Post.rating 值真实。通常我会为此使用数据库触发器,但现在我决定发出 post_save 信号(以减少数据库处理时间):

# vote was saved
@receiver(post_save, sender=Vote)
def update_post_votes(sender, instance, created, **kwargs):
    """ Update post rating """
    if created:
        instance.post.rating += instance.value
        instance.post.save()
    else:
        # if vote was updated, we need to remove the old vote value and add the new one
        # but how...?

如何在保存之前访问实例值?在数据库触发器中,我会为此预定义 OLDNEW,但是 post_save 信号中有类似的东西吗?

更新

基于马克的答案的解决方案:

# vote was saved
@receiver(pre_save, sender=Vote)
def update_post_votes_on_save(sender, instance, **kwargs):
    """ Update post rating """
    # if vote is being updated, then we must remove previous value first
    if instance.id:
        old_vote = Vote.objects.get(pk=instance.id)
        instance.post.rating -= old_vote.value
    # now adding the new vote
    instance.post.rating += instance.value
    instance.post.save()

最佳答案

我认为 post_save 为时已晚,无法检索未修改的版本。顾名思义,此时数据已经写入数据库。您应该改用 pre_save。在这种情况下,您可以通过 pk:old = Vote.objects.get(pk=instance.pk) 从数据库中检索模型,并检查当前实例和前一个实例的差异。

关于python - Django:如何在 post_save 信号中访问原始(未修改)实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5582410/

相关文章:

python - 当方法更改对象 "in place"或返回新对象时是否有任何规则?

python - 如何在 Django 中创建自动递增整数字段?

django - Django Model中的抽象继承,导致MAX递归深度错误

sql - PostgreSQL - 选择条件满足的不同(列1,列2)

postgresql - 如何在不创建函数的情况下执行 pl/pgsql 代码?

python - Pandas 合并(如何 ="inner")结果大于两个数据帧

python - Python 类中的 unicode(self) 和 self.__unicode__() 有什么区别?

python - 如何从 python 中的文件夹中读取多个 NetCDF 文件

django 模板和列表字典

python - 听 channel 名称;由于 ' vs. "而失败