简体   繁体   English

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

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

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.rating value actual.当然,我需要保持Post.rating值实际。 Nornally I would use database triggers for that, but now I've decided to make a post_save signal (to reduce database process time):通常我会为此使用数据库触发器,但现在我决定制作一个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?在数据库触发器中,我会为此预定义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()

I believe post_save is too late to retrieve the unmodified version.我相信post_save来不及检索未修改的版本。 As the name implies the data has already been written to the db at that point.顾名思义,此时数据已经写入数据库。 You should use pre_save instead.您应该改用pre_save 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.在这种情况下,您可以通过 pk 从数据库中检索模型: old = Vote.objects.get(pk=instance.pk)并检查当前实例和先前实例的差异。

This is not an optimal 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 

You can use the FieldTracker from django-model-utils: https://django-model-utils.readthedocs.io/en/latest/utilities.html#field-tracker您可以使用 django-model-utils 中的 FieldTracker: https ://django-model-utils.readthedocs.io/en/latest/utilities.html#field-tracker

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM