[Django] Error when SlugField is specified in .filter ()

Error Related Field got invalid lookup:

Cause

It seems that pk and slug are not included because it is supposed to fetch multiple objects.

Occurrence Status

table

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)

Solution

Change from SlugField to CharField.

Recommended Posts

[Django] Error when SlugField is specified in .filter ()
Display error message when login fails in Django
When "ERROR: HTTP is not supported." Is displayed in mpsyt
CSRF validation error when accessing django 3.0 admin page in Chrome
If you get Error: That port is already in use. In Django
When you can't call base.html in Django
[Django] Error encountered when deploying heroku Part 2
When the target is Ubuntu 16.04 in Ansible
[Django] sqlite version error when running python manage.py in aws cloud9 environment
Permission error when reading Django upload files
Output timing is incorrect when standard (error) output is converted to a file in Python
[Python] What to check when you get a Unicode Decode Error in Django
When I get a chromedriver error in Selenium
When searching is not working in GAE's Datastore
Image URL is blank in GAE & GCS & Django
Error when trying to install psycopg2 in Python
I defined ForeignKey to CustomUser specified in AUTH_USER_MODEL in Django, but it is not referenced
Models in Django
Error in random.shuffle
Error in Pyinstaller
Django filter summary
Error in TensorBoard
Forms in Django
When the selected object in bpy.context.selected_objects is not returned
[Django] Display the error message specified by raise ValidationError
When displaying RichTextField in Wagtail, <p> tag is attached.
When you want to filter with Django REST framework
I get an error when I put opencv in pyautoGUI
Embedding in datetime when only the time is known
When you get an error in python scraping (requests)
Explanation of NoReverseMatch error in "python django super introduction"