I would like to filter subcategories based on the category id from the URL
For a constant value, it works without a problem
return Subcategory.objects.filter(category = 1)
views.py
class SubcategoriesListView(ListView):
model = Subcategory
template_name = 'app/categories/index.html'
def get_queryset(self):
return Subcategory.objects.filter(category = category_id)
urls.py
path('categories/<int:category_id>/', app.views.SubcategoriesListView.as_view(), name='subcategories'),
models.py
class Subcategory(models.Model):
title = models.CharField(max_length=30)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
Traceback
NameError at /categories/1/
name 'category_id' is not defined
views.py in get_queryset
return Subcategory.objects.filter(category = category_id)
You can obtain the URI positional and named parameters in a class-based view with self.args
(a tuple) and self.kwargs
(a dictionary) respectively.
Here you defined the category_id
as a named parameter, so you can obtain its corresponding value with self.kwargs['category_id']
:
class SubcategoriesListView(ListView):
model = Subcategory
template_name = 'app/categories/index.html'
def get_queryset(self):
return Subcategory.objects.filter(category_id=self.kwargs['category_id'])
Since the id
is an integer, you thus filter on category_id
, not on category
.
This is detailed in the "dynamic filtering" section of Django's documentation on Built-in class-based generic views.