I've been struggling with dajngo-filter recently. I have 2 models: Book and Author.
class Book(models.Model):
title = models.CharField(max_length=150)
author = models.ForeignKey(Author, on_delete=model.CASCADE)
length = models.PositiveIntegerField()
class Author(models.Model):
first_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
year_of_birth = models.DateFiled()
So, I'd like to make filter page, where you can find book by it's properties (title, author etc) AND information about author. For example book shorter than 300 pages, which author is beetween 20-25 years old.
In filters.py I have:
import django_filters
from .models import Book, Author
class BookFilter(django_filters.FilterSet):
class Meta():
model = Book
fields = ['title', 'author']
And have actually no idea how to also filter by different model, which is related by ForeignKey to Book model.
I have really little experience with Dajngo, I wonder if it's very complex problem or not. :) Thanks!