相关名称返回 post.commentpost.none

Related name returning post.commentpost.none

谁能解释一下,为什么我得到 Post.CommentPost.None?

如何正确连接查询中的CommentPost和Posty?我需要得到 {{posty.comments.user}} 我的结果是 Post.CommentPost.None

这是关于我的模型和函数的一些信息。

class Posty(models.Model):
   title = models.CharField(max_length=250, blank=False, null=False, unique=True)
   sub_title = models.SlugField(max_length=250, blank=False, null=False, unique=True)
   content = models.TextField(max_length=250, blank=False, null=False)
   image = models.ImageField(default="avatar.png",upload_to="images", validators=[FileExtensionValidator(['png','jpg','jpeg'])])
   author = models.ForeignKey(Profil, on_delete=models.CASCADE)
   updated = models.DateTimeField(auto_now=True)
   published = models.DateTimeField(auto_now_add=True)
   T_or_F = models.BooleanField(default=False)
   likes = models.ManyToManyField(Profil, related_name='liked')
   unlikes = models.ManyToManyField(Profil, related_name='unlikes')
   created_tags = models.ForeignKey('Tags', blank=True, null=True, related_name='tagi', on_delete=models.CASCADE)
  test_wyswietlenia = models.IntegerField(default=0, null=True, blank=True)

class CommentPost(models.Model):
   user = models.ForeignKey(Profil, on_delete=models.CASCADE)
   post = models.ForeignKey(Posty, on_delete=models.CASCADE, related_name="comments")
   content1 = models.TextField(max_length=250, blank=False, null=False)
   date_posted = models.DateTimeField(default=timezone.now)
   date_updated = models.DateTimeField(auto_now=True)

观看次数

     tag = request.GET.get('tag')
   if tag == None:
       my_tag = Posty.objects.prefetch_related('comments')
       my_view = Posty.objects.prefetch_related('my_wyswietlenia')

   else:
      my_tag = Posty.objects.filter(created_tags__tag=tag)
      my_view = Posty.objects.prefetch_related('my_wyswietlenia')

模板

                {% for post in my_tag %}
                  {% if post.comments.last.user == None %}
                    <span class="forum_tag_author">Komentarz » <a href="{% url 'home:detail_post' post.pk %}">Brak komentarza</a></span><br/>
                    <span class="forum_tag_author">Stworzony przez » <a href="{% url 'profile:profil_uzytkownika' post.pk %}">{{post.author}} </a> </span><hr/>
                  {% else %}
                    <span class="forum_tag_author">Komentarz » <a href="{% url 'home:detail_post' post.pk %}">{{post.comments.last.content1}}</a></span><br/>
                    <span class="forum_tag_author">Odpowiadający » <a href="{% url 'profile:profil_uzytkownika' post.pk %}">Dodany przez: {{post.comments.last.user}} </a> </span><hr/>
                  {% endif %}
              {% endfor %}

还有这个 {{post.comments}}提出要求Post.CommentPost.None

有什么问题?我做错了什么?

您可以使用 Subquery expression [Django-doc] 获取最新的 content1 评论和作者:

from django.db.models import OuterRef, Subquery

last_comment = CommentPost.objects.filter(post=OuterRef('pk')).order_by('-date_posted')

my_tag = Posty.objects.annotate(
    <strong>last_comment=Subquery(</strong>last_comment.values('content1')[:1]<strong>)</strong>,
    <strong>last_comment_user=Subquery(</strong>last_comment.values('user__name')[:1]<strong>)</strong>,
    <strong>last_comment_user_pk=Subquery(</strong>last_comment.values('user')[:1]<strong>)</strong>
).prefetch_related('my_wyswietlenia')

__name 可能会有所不同,因为它取决于您 Profil 模型的领域。

然后你可以渲染内容和最后一个作者:

{% for post in my_tag %}
    {{ post<strong>.last_comment</strong> }} by {{ post<strong>.last_coment_user</strong> }}
{% enfor %}