gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 21:55:03 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com