Answer the question
In order to leave comments, you need to log in
How can you filter objects when using select_related()?
I have classes Category and Article. I need to display PUBLISHED articles when selecting them by category.
Code from views.py:
class CategoryDetailView(DetailView):
model = Category
def get_context_data(self, **kwargs):
context = super(CategoryDetailView, self).get_context_data(**kwargs)
context['article_list'] = Article.objects.select_related()
return context
{% for article in object.article_set.all %}
{{ article.title }}
{{ article.author }}
{{ article.published }}
{% endfor %}
context['article_list'] = Article.objects.select_related().filter(status=1)
nothing happens, unfortunately. Answer the question
In order to leave comments, you need to log in
at this point
you are not accessing the article_list variable, you are simply iterating over all the articles that are in the category.
Make your own article manager like this
class ArticleManager(models.Manager):
def all_published(self):
return self.get_queryset().filter(status=1)
def get_context_data(self, **kwargs):
in this case :)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question