У меня возникает ошибка "Related Field got invalid lookup: icontains", когда я пытаюсь фильтровать на views.py из models.py
здесь, в views.py Я хочу отфильтровать 'language' из models.py но он говорит, что есть ошибка. "raise FieldError('Related Field got invalid lookup: {}'.format(lookup_name)) django.core.exceptions.FieldError: Related Field got invalid lookup: icontains"
Я не знаю, почему проблема только с "языком". но не когда я пытаюсь фильтровать 'genre', 'title', я не вижу ошибку на терминале какие могут быть решения?
здесь models.py
from django.db import models
from django.urls import reverse
import uuid
class Genre(models.Model):
name = models.CharField(max_length=200, help_text='Enter a book genre (e.g. Science Fiction)')
def __str__(self):
return self.name
class Language(models.Model):
name = models.CharField(max_length=200,
help_text="Enter the book's natural language (e.g. English, French, Japanese etc.)")
def __str__(self):
return self.name
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.ForeignKey('Author', on_delete=models.SET_NULL, null=True)
summary = models.TextField(max_length=1000, help_text='Enter a brief description of the book')
isbn = models.CharField('ISBN', max_length=13, help_text='13 Character <a href="https://www.isbn-international.org/content/what-isbn">ISBN number</a>')
genre = models.ManyToManyField(Genre, help_text='Select a genre for this book')
language = models.ForeignKey('Language', on_delete=models.SET_NULL, null=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('book-detail', args=[str(self.id)])
class BookInstance(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, help_text='Unique ID for this particular book across whole library')
book = models.ForeignKey('Book', on_delete=models.SET_NULL, null=True)
imprint = models.CharField(max_length=200)
due_back = models.DateField(null=True, blank=True)
LOAN_STATUS = (
('m', 'Maintenance'),
('o', 'On loan'),
('a', 'Available'),
('r', 'Reserved'),
)
status = models.CharField(
max_length=1,
choices=LOAN_STATUS,
blank=True,
default='m',
help_text='Book availability',
)
class Meta:
ordering = ['due_back']
def __str__(self):
return f'{self.id} ({self.book.title})'
class Author(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
date_of_birth = models.DateField(null=True, blank=True)
date_of_death = models.DateField('Died', null=True, blank=True)
def get_absolute_url(self):
return reverse('author-detail', args=[str(self.id)])
def __str__(self):
return f'{self.last_name}, {self.first_name}'
class Meta:
ordering = ['last_name']
это views.py
from django.shortcuts import render
from .models import Book, Author, BookInstance, Genre
from django.views import generic
def index(request):
num_books = Book.objects.all().count()
num_instances = BookInstance.objects.all().count()
num_instances_available = BookInstance.objects.filter(status__exact='a').count()
num_authors = Author.objects.count()
context = {
'num_books': num_books,
'num_instances': num_instances,
'num_instances_available': num_instances_available,
'num_authors': num_authors,
}
return render(request, 'index.html', context=context)
class BookListView(generic.ListView):
model = Book
template_name = "catalog/book_list.html"
queryset = Book.objects.filter(language__icontains='Hebrew')
class BookDetailView(generic.ListView):
model = Book
class BokListView(generic.ListView):
model = Book
template_name = "catalog/bok_list.html"
class BokDetailView(generic.ListView):
model = Book
class BkListView(generic.ListView):
model = Book
template_name = "catalog/bk_list.html"
class BkDetailView(generic.ListView):
model = Book
class AuthorListView(generic.ListView):
model = Author
class AuthorDetailView(generic.ListView):
model = Author
Замените queryset = Book.objects.filter(language__icontains='Hebrew')
на queryset = Book.objects.filter(language__name__icontains='Hebrew')
Вы должны указать поле, по которому ведется поиск.