Answer the question
In order to leave comments, you need to log in
How to display related objects in a separate ListView?
Good afternoon!
I can't figure out how to do it right.
Let's say we have the following models:
class Post(models.Model):
...
class Comment(models.Model):
...
post = models.ForeignKey(Post)
class PostListView(ListView):
model = Post
# views.py
class CommentListView(ListView):
model = Comment
def get_queryset(self):
return Comment.objects.filter(post=self.kwargs.get('post_pk'))
# urls.py
url(r'^post/(?P<post_pk>\d+)/comments/$', CommentListView.as_view(), name='comment-list')
Answer the question
In order to leave comments, you need to log in
Well, yes. Add the rest manually in get_context_data.
By the way, I advise you to replace get_queryset with:
def get_queryset(self):
return super().get_queryset().filter(post=self.kwargs.get('post_pk'))
If I understood you correctly, then for /countries/1/regions/3/city/2 in urls.py there should be something like
And redo the get_queryset method accordingly
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question