gpt4 book ai didi

Django 休息 update_or_create

转载 作者:行者123 更新时间:2023-12-02 09:37:00 28 4
gpt4 key购买 nike

背景:

我正在尝试创建一个系统,允许用户对其他用户撰写的评论进行投票(类似于 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')

序列化器.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)

最佳答案

欢迎来到 StackOverflow!

您的问题在 CommentManager 中:

queryset.order_by('-upvotedownvotes__vote')

此查询基本上创建LEFT_OUTER_JOIN。所以你的结果看起来像:

comment#1 upvote#1
comment#1 upvote#2
comment#1 upvote#3

这就是您看到评论#1 3 次的原因。

我相信你想使用这样的东西:https://docs.djangoproject.com/en/2.1/topics/db/aggregation/#order-by

关于Django 休息 update_or_create,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53340774/

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