让这些模型:
class Category(models.Model):
name = models.CharField(max_length=20)
class Word(models.Model):
name = models.CharField(max_length=200)
categories = models.ManyToManyField(Category, null=True, blank=True)
这个观点:
def main_page(request):
words = Word.objects.all()
return render(request, "main_page.html", {'words': words})
如何按类别将类别作为参数传递来过滤单词?
答案 0 :(得分:1)
您可以使用反向关系:
{{ category.word_set.all }}