0

私は自分のdjangoアプリケーション用のコメントシステムを作っています。コメント投票用に別々のモデルを作るのが最善だと言われました。だから私はそれをやったし、ここで私のmodels.pyです:私のテンプレートにForeignKeyフィールドをレンダリングできません

def article(request, category, id): 

    name = resolve(request.path).kwargs['category'] 
    for a, b in CATEGORY_CHOICES: 
     if b == name: 
      name = a 
      instance = get_object_or_404(Post, id=id, category=name) 

    allauth_login = LoginForm(request.POST or None) 
    allauth_signup = SignupForm(request.POST or None) 


    #comments 
    comment = CommentForm(request.POST or None) 
    ajax_comment = request.POST.get('text') 
    comment_length = len(str(ajax_comment)) 

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

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

      score = CommentScore.objects.create(comment=comment) 
      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, 
     'comment': comment, 
     'instance': instance, 
     'allauth_login': allauth_login, 
     'allauth_signup': allauth_signup 
    } 

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

だからcommentが正常に動作しますが、よう:

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) 

ここでコメントが作成され、私のviews.pyです私はその後、インスタンスを作成してcommentと一致させようとしています。私のテンプレートでは、私は各コ​​メントとそれのフィールド(comment_textauthorなど)をレンダリングしましたが、upvotesフィールドをそれに関連付けようとしていますcomment。どうすればいい?

テンプレート

{% for i in comment_list %} 
    <div class='comment_div'> 
     <h3>{{ i.author }}</h3> 
     <p>{{ i.comment_text }}</p><br> 
    </div> 
{% endfor %} 

forms.py

class CommentForm(forms.ModelForm): 
    class Meta: 
     model = Comment 

     fields = [ 
      'comment_text', 
      'id', 
      'author', 
      'destination', 
     ] 

私はすでに次のことを試してみた、彼らが働いていません。

{% for i in comment_list %} 
    <div class='comment_div'> 
     <h3>{{ i.author }}</h3> 
     <p>{{ i.comment_text }}</p><br> 
      {% for i in comment_list.score_set.all %} 
       {{ i.upvotes }} #renders nothing 
      {% endfor %} 
    </div> 
{% endfor %} 

{% for i in comment_list %} 
    <div class='comment_div'> 
     <h3>{{ i.author }}</h3> 
     <p>{{ i.comment_text }}</p><br> 
      {% for j in i.score %} 
       {{ j.upvotes }} #Error: 'RelatedManager' object is not iterable 
      {% endfor %} 
    </div> 
{% endfor %} 

多くの問題を抱えているので、助けていただければ幸いです。

+0

"j.upvotes"を "j.upvotes.all"に変更してみてください –

+0

同じエラーが表示されます。 – Zorgan

+1

「i.score」を「i.score.all」に変更してみてください。そのManagerによって選択されたオブジェクトではなく、マネージャ上で反復しようとすると、通常、RelatedManaagerエラーが発生します。 –

答えて

2

"i.score"を "i.score.all"に変更すると、通常マネージャで反復しようとしているときにRelatedManaagerエラーが発生するため、問題が解決されます。 - @のジョー-J

が解決しようだから、それが機能するようになりました誰かが素晴らしいことだ、この構文の第二ライン説明できる場合:

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

を私はここにcomment=comment_listを割り当てる際に、正確に何が起こっていますか?私はこのコードを他の誰かからコピーしましたが、私はまだそれがどのように動作しているのか不明です。

関連する問題