【问题标题】:Related name returning post.commentpost.none相关名称返回 post.commentpost.none
【发布时间】:2022-01-04 21:09:32
【问题描述】:

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

如何在查询中正确连接 CommentPost 和 Posty?我需要得到 {{posty.cmets.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.cmets}} 发出请求 Post.CommentPost.None

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

【问题讨论】:

  • .comments 是经理,你可以使用{{ post.comments.all }},但我建议在视图中注释查询集以获得最后一个作者和content1 更快。
  • 当我这样做时,我得到了所有 cmets 列表。当我只需要时,帖子名称,最后一个帖子,添加最后评论到帖子的用户
  • 当然,这就是您需要注释而不是预取的原因。

标签: django post django-queryset


【解决方案1】:

您可以使用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(
    last_comment=Subquery(last_comment.values('content1')[:1]),
    last_comment_user=Subquery(last_comment.values('user__name')[:1]),
    last_comment_user_pk=Subquery(last_comment.values('user')[:1])
).prefetch_related('my_wyswietlenia')

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

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

{% for post in my_tag %}
    {{ post.last_comment }} by {{ post.last_coment_user }}
{% enfor %}

【讨论】:

  • 我还能再问一个问题吗?如何使用 PK 创建 URL 以重定向到最后评论用户配置文件。主要是我有这个问题。主要问题是这里没有阅读 CommentPost 部分,而 Post.CommentPost.None 又回来了
  • @ProtoN:您可以注释最后一个用户的主键(见编辑),然后使用它来计算 URL。
  • 你的意思是 last_comment_user?
  • @ProtoN: 不,last_comment_user_pk(见编辑,将其添加到注释中)
  • 不,只是{% url 'profile:profil_uzytkownika' post.last_comment_user_pk %}。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-29
相关资源
最近更新 更多