So, I have this class and would like to filter the list of quizzes according to categories (which is a foreignkey). I would like for the view to only display quizzes for each of the categories separately such as 'History', 'Chemistry' and so on.
class QuizListView(generic.ListView):
#model = Quiz
queryset = Quiz.objects.filter(Category='History')
Models:
class Quiz(models.Model):
title = models.CharField(
verbose_name=_("Titulli"),
max_length=60, blank=False)
description = models.TextField(
verbose_name=_("Përshkrimi"),
blank=True, help_text=_("a description of the quiz"))
url = models.SlugField(
max_length=60, blank=False,
help_text=_("a user friendly url"),
verbose_name=_("user friendly url"))
category = models.ForeignKey(
Category, null=True, blank=True,
verbose_name=_("Kategoria"), on_delete=models.CASCADE)
the category class:
class Category(models.Model):
category = models.CharField(
verbose_name=_("Category"),
max_length=250, blank=True,
unique=True, null=True)
objects = CategoryManager()
class Meta:
verbose_name = _("Kategori")
verbose_name_plural = _("Kategoritë")
def __str__(self):
return self.category
THis is the error on the terminal:
Cannot resolve keyword 'quiz_category' into field. Choices are: answers_at_end, categor y, category_id, data_postimit, description, draft, exam_paper, fail_text, id, max_questions, pass_mark, question, random_o rder, single_attempt, sitting, success_text, title, url
tried other ways but can't seem to figure it out, any help highly appreciated
category
refers to a Category
object, if you want to filter on the value of the category
field of the related category
object, you use double underscores (__
), so:
class QuizListView(generic.ListView):
model = Quiz
queryset = Quiz.objects.filter(category__category='History')