如何return最后评论?
How to return last comment?
我可能有一个小问题。即当我使用 'post.comments.all'
时如何提取最后一条评论
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)
def __str__(self):
return str(self.content1)
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)
观看次数
tag = request.GET.get('tag')
if tag == None:
my_tag = Posty.objects.all
print(my_tag)
else:
my_tag = Posty.objects.filter(created_tags__tag=tag)
print(my_tag)
如果我尝试使用“[:1]”或最后一个,这不起作用。
您可以通过以下方式获取最后一条评论:
post.comments<strong>.latest('date_posted')</strong>
或者如果您想要每个 date_updated
的最新版本,您可以使用:
post.comments<strong>.latest('date_updated')</strong>
如果您想获得每个 post 的最后一条评论,您可以使用 Prefetch
对象:
from django.db.models import <strong>OuterRef, Subquery</strong>
posts = Posty.objects.annotate(
latest_comment=<strong>Subquery(</strong>
CommentPost.objects.filter(
post=OuterRef('pk')
).values('content1').order_by('-date_posted')[:1]
<strong>)</strong>
)
然后在模板中,您可以渲染:
{% for post in posts %}
{{ post<strong>.last_comment</strong> }}
{% endfor %}
我可能有一个小问题。即当我使用 'post.comments.all'
时如何提取最后一条评论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)
def __str__(self):
return str(self.content1)
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)
观看次数
tag = request.GET.get('tag')
if tag == None:
my_tag = Posty.objects.all
print(my_tag)
else:
my_tag = Posty.objects.filter(created_tags__tag=tag)
print(my_tag)
如果我尝试使用“[:1]”或最后一个,这不起作用。
您可以通过以下方式获取最后一条评论:
post.comments<strong>.latest('date_posted')</strong>
或者如果您想要每个 date_updated
的最新版本,您可以使用:
post.comments<strong>.latest('date_updated')</strong>
如果您想获得每个 post 的最后一条评论,您可以使用 Prefetch
对象:
from django.db.models import <strong>OuterRef, Subquery</strong>
posts = Posty.objects.annotate(
latest_comment=<strong>Subquery(</strong>
CommentPost.objects.filter(
post=OuterRef('pk')
).values('content1').order_by('-date_posted')[:1]
<strong>)</strong>
)
然后在模板中,您可以渲染:
{% for post in posts %}
{{ post<strong>.last_comment</strong> }}
{% endfor %}