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

时间:2022-05-12 22:48:08

I want to do a data denormalization for better performance, and put a sum of votes my blog post receives inside Post model:

我想做一个数据去规格化以获得更好的性能,并将我的博客文章收到的投票汇总到内文模型中:

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()

Ofcourse, I need to keep Post.rating value actual. Nornally I would use database triggers for that, but now I've decided to make a post_save signal (to reduce database process time):

当然,我得守岗位。评级的实际价值。Nornally为此使用了数据库触发器,但现在我决定发出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...?

How can I access the instance value before it was saved? In database triggers, i would have OLD and NEW predefines for this, but is there something like this in post_save signals?

如何在保存实例值之前访问它?在数据库触发器中,我将使用旧的和新的预定义,但是在post_save信号中是否有类似的东西?

UPDATE

更新

The solution based on Mark's the answer:

基于Mark的解决方案:

# 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()

1 个解决方案

#1


40  

I believe post_save is too late to retrieve the unmodified version. As the name implies the data has already been written to the db at that point. You should use pre_save instead. In that case you can retrieve the model from the db via pk: old = Vote.objects.get(pk=instance.pk) and check for differences in the current instance and the previous instance.

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

#1


40  

I believe post_save is too late to retrieve the unmodified version. As the name implies the data has already been written to the db at that point. You should use pre_save instead. In that case you can retrieve the model from the db via pk: old = Vote.objects.get(pk=instance.pk) and check for differences in the current instance and the previous instance.

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