It seems that pk and slug are not included because it is supposed to fetch multiple objects.
Authors: Authors Have multiple books. Books: Has one author.
models.py
python
class Authors(models.Model):
name = models.CharField(max_length=255)
slug = models.SlugField(unique=True, max_length=255)
class Books(models.Model):
title = models.CharField(max_length=255)
author = models.ForeignKey(Authors, on_delete=models.PROTECT)
views.py
python
slug = 'dazai'
books = Books.objects.filter(author__slug=slug)
Change from SlugField to CharField.
Recommended Posts