背景:
我正在尝试创建一个系统,允许用户对其他用户的评论进行投票(类似于Reddit)。用户可以选择三个投票值:-1、0或1。我创建了一个POST API(使用django rest-framework),用于存储用户对特定评论的投票。如果用户已经对给定的评论进行了投票,那么它将把现有用户的投票值更新为新的。
我从这篇文章中汲取了灵感:Django RF update_or_create
问题:
一旦评论使一个用户对它进行投票,Django就会在另一位用户对同一评论投票时创建具有相同ID /主键的重复评论对象。我拍了我的管理页面的屏幕截图,其中说我有3个对象可供选择,但只有一个注释。为什么这样做,我该如何预防呢?
Screenshot of my comment admin page
我是Django的新手。我怀疑在序列化器中定义自己的“创建”方法时可能做错了。我将不胜感激。谢谢!
models.py :
评论模型:
class Comment(models.Model):
location_property_category = models.ForeignKey('locations.LocationPropertyCategory',on_delete=models.CASCADE,related_name='comments',null=True)
author = models.ForeignKey('auth.User',on_delete=models.PROTECT,related_name='comments')
location = models.ForeignKey('locations.Location',on_delete=models.CASCADE,related_name='comments')
text = models.TextField()
create_date = models.DateTimeField(default=timezone.now())
published_date = models.DateTimeField(blank=True,null=True)
approved_comment = models.BooleanField(default=False)
objects = CommentManager()
def approve(self):
self.approved_comment = True
self.save()
def __str__(self):
return self.text
def save(self, *args, **kwargs):
if self.approved_comment is True:
self.published_date = timezone.now()
super(Comment, self).save(*args, **kwargs)
def sum_vote(self):
return self.upvotedownvotes.aggregate(Sum('vote')).get('vote__sum') or 0
投票模型:
class UpVoteDownVote(models.Model):
UPVOTE = 1
NEUTRALVOTE = 0
DOWNVOTE = -1
VOTES = (
(UPVOTE, 'Upvote'),
(NEUTRALVOTE, 'Neutralvote'),
(DOWNVOTE, 'Downvote')
)
vote = models.SmallIntegerField(choices=VOTES)
user = models.ForeignKey('auth.User', related_name='upvotedownvotes', on_delete=models.CASCADE)
comment = models.ForeignKey(Comment, related_name='upvotedownvotes', on_delete=models.CASCADE)
date_voted = models.DateTimeField(default=timezone.now())
class Meta:
unique_together = (('user','comment'),)
评论管理器模型:
class CommentManager(models.Manager):
def get_queryset(self):
return super(CommentManager, self).get_queryset().order_by('-upvotedownvotes__vote')
serializers.py 投票序列化器:
class UpVoteDownVoteSerializer(serializers.ModelSerializer):
class Meta:
model = UpVoteDownVote
fields = ('vote','comment')
def get_fields(self):
fields = super(UpVoteDownVoteSerializer, self).get_fields()
fields['comment'].queryset = Comment.objects.filter(approved_comment=True)
return fields
def create(self, validated_data):
votedata, created = UpVoteDownVote.objects.update_or_create(
user=validated_data.get('user', None),
comment=validated_data.get('comment', None),
defaults={'vote': validated_data.get('vote', None),
})
return votedata
views.py
class UpVoteDownVoteCreateApiView(generics.CreateAPIView):
serializer_class = UpVoteDownVoteSerializer
permission_classes = [IsAuthenticated]
def perform_create(self,serializer):
serializer.save(user=self.request.user)
评论应用admin.py
class CommentAdmin(admin.ModelAdmin):
readonly_fields = ('id',)
admin.site.register(Comment,CommentAdmin)
答案 0 :(得分:0)
欢迎使用StackOverflow!
您的问题出在CommentManager
:
queryset.order_by('-upvotedownvotes__vote')
此查询基本上创建了LEFT_OUTER_JOIN
。这样您的结果看起来像:
comment#1 upvote#1
comment#1 upvote#2
comment#1 upvote#3
这就是为什么您看到3条评论#1的原因。
我相信您想使用类似以下的内容:https://docs.djangoproject.com/en/2.1/topics/db/aggregation/#order-by