1👍
✅
You can filter with:
class ResultsView(FormMixin, DetailView):
model = Post
template_name = 'posts.html'
form_class = CommentForm
def get_context_data(self, **kwargs):
return super().get_context_data(
**kwargs, comms=self.object.comments.filter(is_active=True)
)
and then render with:
{% for comment in comms %}
# …
{% endfor %}
Note: It is normally better to make use of the
settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use theUser
model [Django-doc] directly. For more information you can see the referencing theUser
model section of the documentation.
Note: The
related_name=…
parameter [Django-doc]
is the name of the relation in reverse, so from theUser
model to theComment
model in this case. Therefore it (often) makes not much sense to name it the
same as the forward relation. You thus might want to consider renaming therelation toauthor
comments
.
Source:stackexchange.com