誰能解釋我,為什么我得到 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
什么是問題?我做什么壞事?
uj5u.com熱心網友回復:
您可以使用Subquery運算式 [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 %}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/406576.html
標籤:
