Sil*_*ght 35 python django postgresql django-signals denormalization
我想做一个数据非规范化以获得更好的性能,并在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()
Run Code Online (Sandbox Code Playgroud)
当然,我需要保持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...?
Run Code Online (Sandbox Code Playgroud)
如何在保存之前访问实例值?在数据库触发器,我会OLD
和NEW
这个预定义,但有在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()
Run Code Online (Sandbox Code Playgroud)
Mar*_*vin 52
我认为post_save
检索未经修改的版本为时已晚.顾名思义,此时数据已经写入数据库.你应该使用pre_save
.在这种情况下,您可以通过pk从db中检索模型,old = Vote.objects.get(pk=instance.pk)
并检查当前实例和上一个实例的差异.
Par*_*us- 19
这不是最佳解决方案,但它有效。
@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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
17123 次 |
最近记录: |