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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5582410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-20 22:56:49  来源:igfitidea点击:

Django: How to access original (unmodified) instance in post_save signal

pythondjangopostgresqldenormalizationdjango-signals

提问by Silver Light

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

我想对数据进行非规范化以获得更好的性能,并将我的博客文章收到的总投票数放入 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()

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

当然,我需要保持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...?

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

如何在保存之前访问实例值?在数据库触发器,我会OLDNEW这个预定义,但有在post_save信号,这样的事情?

UPDATE

更新

The solution based on Mark's the answer:

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

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

回答by Mark Lavin

I believe post_saveis 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_saveinstead. 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检索未修改的版本为时已晚。顾名思义,此时数据已经写入数据库。你应该pre_save改用。在这种情况下,您可以通过 pk: 从数据库中检索模型,old = Vote.objects.get(pk=instance.pk)并检查当前实例和前一个实例的差异。

回答by Parag Tyagi -morpheus-

This is not anoptimal solution, but it works.

不是最佳解决方案,但它有效。

@receiver(pre_save, sender=SomeModel)
def model_pre_save(sender, instance, **kwargs):
    try:
        instance._pre_save_instance = SomeModel.objects.get(pk=instance.pk)
    except SomeModel.DoesNotExist:
        instance._pre_save_instance = instance


@receiver(signal=post_save, sender=SomeModel)
def model_post_save(sender, instance, created, **kwargs):
    pre_save_instance = instance._pre_save_instance
    post_save_instance = instance