2011-04-07 31 views
26

我想要做一个数据的非规范化以提高性能,并把我的博客文章接收Post模型内的票数总和访问原始(未修改)实例:Django的:如何在post_save信号

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,我需要保持Post.rating的实际值。 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...? 

我如何可访问对象的值保存它之前?在数据库触发器中,我会为此预定义OLDNEW,但在post_save信号中是否有这样的内容?

UPDATE

根据马克的答案解决办法:

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

回答

38

我相信post_save是来不及检索未修改的版本。顾名思义,数据已经被写入数据库。您应该使用pre_save。在这种情况下,您可以通过pk:old = Vote.objects.get(pk=instance.pk)从db中检索模型,并检查当前实例和以前实例的差异。

+0

谢谢,你是绝对正确的。结果是,当instans被创建时,它在pre_save信号中没有'id',但是如果它正在更新,它有一个'id'。这正是我所需要的,请参阅实际解决方案的问题更新。 – 2011-04-08 11:24:09

相关问题