Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Related name returning post.commentpost.none

Can anyone explain me, why i get Post.CommentPost.None?

How to correct connect CommentPost with Posty in query? I need get {{posty.comments.user}} my result is Post.CommentPost.None

Here something about my models and functions.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

VIEWS

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

TEMPLATES

                {% 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 %}

And this
{{post.comments}} give a request Post.CommentPost.None

What is problem? What i do bad?

>Solution :

You can use a Subquery expression [Django-doc] to obtain the latest content1 comment and author with:

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])
).prefetch_related('my_wyswietlenia')

The __name might be different, since it depends on the fields of you Profil model.

Then you can render the content and the last author with:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading