Python 创建新实例时如何分配ForeignKey字段

Python 创建新实例时如何分配ForeignKey字段,python,django,django-models,Python,Django,Django Models,我正在建立一个评论系统,并试图实现投票。因此,在comment=comment.objects.create(comment\u text=ajax\u comment,author=str(request.user),destination=id)行中,我创建了一个新的comment对象,我想添加一个upvows字段。因此,当创建对象时,upvows=0。我该怎么做 型号.py class Comment(models.Model): user = models.ForeignKey(

我正在建立一个评论系统,并试图实现投票。因此,在
comment=comment.objects.create(comment\u text=ajax\u comment,author=str(request.user),destination=id)
行中,我创建了一个新的comment对象,我想添加一个upvows字段。因此,当创建对象时,
upvows=0
。我该怎么做

型号.py

class Comment(models.Model):
    user = models.ForeignKey(User, default=1)
    destination = models.CharField(default='1', max_length=12, blank=True)
    author = models.CharField(max_length=120, blank=True)
    comment_id = models.IntegerField(default=1)
    parent_id = models.IntegerField(default=0)
    comment_text = models.TextField(max_length=350, blank=True)
    timestamp = models.DateTimeField(default=timezone.now, blank=True)

    def __str__(self):
        return self.comment_text


class CommentScore(models.Model):
    user = models.ForeignKey(User, default=1)
    comment = models.ForeignKey(Comment, related_name='score')
    upvotes = models.IntegerField(default=0)
    downvotes = models.IntegerField(default=0)

    def __str__(self):
        return str(self.comment)
comment_list = Comment.objects.filter(destination=id)
score = CommentScore.objects.all()

if request.is_ajax():
    if comment.is_valid():
        comment = Comment.objects.create(comment_text=ajax_comment, author=str(request.user), destination=id)
        print(comment)
        comment.save()
        username = str(request.user)
        return JsonResponse({'text': ajax_comment, 'text_length': comment_length, 'username': username})
    else:
        print(comment.errors)

context = {
    'score': score,
    'comment_list': comment_list,
}

return render(request, 'article.html', context)
视图.py

class Comment(models.Model):
    user = models.ForeignKey(User, default=1)
    destination = models.CharField(default='1', max_length=12, blank=True)
    author = models.CharField(max_length=120, blank=True)
    comment_id = models.IntegerField(default=1)
    parent_id = models.IntegerField(default=0)
    comment_text = models.TextField(max_length=350, blank=True)
    timestamp = models.DateTimeField(default=timezone.now, blank=True)

    def __str__(self):
        return self.comment_text


class CommentScore(models.Model):
    user = models.ForeignKey(User, default=1)
    comment = models.ForeignKey(Comment, related_name='score')
    upvotes = models.IntegerField(default=0)
    downvotes = models.IntegerField(default=0)

    def __str__(self):
        return str(self.comment)
comment_list = Comment.objects.filter(destination=id)
score = CommentScore.objects.all()

if request.is_ajax():
    if comment.is_valid():
        comment = Comment.objects.create(comment_text=ajax_comment, author=str(request.user), destination=id)
        print(comment)
        comment.save()
        username = str(request.user)
        return JsonResponse({'text': ajax_comment, 'text_length': comment_length, 'username': username})
    else:
        print(comment.errors)

context = {
    'score': score,
    'comment_list': comment_list,
}

return render(request, 'article.html', context)

您要做的基本上是创建一个新的
CommentScore
对象,然后将其映射到您的
Comment
。这是可以做到的

comment_list = Comment.objects.filter(destination=id)
score = CommentScore.objects.all()

if request.is_ajax():
    if comment.is_valid():
        comment = Comment.objects.create(comment_text=ajax_comment, author=str(request.user), destination=id)
        print(comment)
        comment.save()
        # This line creates a CommentScore model which maps to your Comment model
        com_score = CommentScore.objects.create(comment=comment)
        com_score.save()
        username = str(request.user)
        return JsonResponse({'text': ajax_comment, 'text_length': comment_length, 'username': username})
    else:
        print(comment.errors)

context = {
    'score': score,
    'comment_list': comment_list,
}

return render(request, 'article.html', context)
“向上投票”和“向下投票”将在创建注释对象后发生。因此,一旦做出评论,然后另一个用户到达该对象并选择up vote或down vote,您的前端将不得不调用后端,使用评论对象的键创建CommentScore对象。从本质上讲,将要发生的是:

Comment (object) <- CommentScore (object) 
              ^- CommentScore (object)

upvows
字段已经位于
CommentScore
模型中。你说的“添加一个UpVoces字段”是什么意思?我被告知要制作一个单独的投票模型,因为它会导致人们对一条评论进行多次投票,并监督一个人投票的次数。所以我做到了,现在我正在尝试合并这两个模型。因此,当创建
评论时,我想从
CommentScore
添加
ForeignKey
以添加
upvoces=0
字段,并知道是谁在
CommentScore
中的
user
字段中对文章进行了投票。有什么想法吗?在创建了评论对象之后,“向上投票”和“向下投票”就会出现。因此,一旦发表评论,然后另一个用户进入该对象并选择up vote或down vote,您的前端将不得不调用后端,使用评论对象的键创建CommentScore对象。Zorgan,您有两个很好的答案,请向上投票,如果他们解决了您的问题,请接受。我已经向上投票,我将仔细研究它们,看看哪一个效果最好。在我看来,OP的问题是创建
CommentScore
,而不是您演示的检索。创建如上面的示例所示,即文章->CommentScore。很抱歉,刚刚注意到。干杯!:)谢谢,我看了这个例子,非常有帮助。但是如何显示与每个唯一的
注释相关联的
upvowers
?CommentScore.comment.author可以从我的评论模型中获取
author
字段。但另一方面,我正试图实现的目标又是什么呢?即获取与每个评论相关联的upvotes。或者我应该换一种方式,让文章像评论一样,记者像评论分数一样。这样我就可以做评论、得分、投票了?不,你不需要反过来做。这里有一个例子:`CommentObject=Comment.objects.filter(destination=id)这看起来很好,所以我现在就来看看,并尝试让它工作起来。因此,如果我执行
com\u score=CommentScore.objects.create(comment=comment,upvows=0)
。然后如何在模板中显示每个评论的投票数?(我已在编辑中粘贴了当前模板)