Good day for all!
My app used django class-based generic list view. I have two model objects: Books and Publishers that linked via foreign key (code below). I want to use ListView to show publishers with their books, but filter books (get only active books, owned by current user)
Additional info: I don't want to use filter in template if it's possible. Additional info 2: I can't use filter via define in model class because I need access to request object
code
models.py
class Publisher(models.Model):
name = models.CharField(max_length=255)
class Book(models.Model):
name = models.CharField(max_length=255)
active = models.BooleanField(default=True)
publisher = models.ForeignKey(Publisher, related_name='books')
owner = models.ForeignKey(User)
views.py
class ListBooksByPublisher(ListView):
model = Publisher
template_name = 'list.html'
context_object_name = 'books'
list.html
{% for publisher in publishers %}
{{ publisher.name }}
{% for book in publisher.books.all %}
{{ book.name }}
{% endfor %}
{% endfor %}
Any help much appreciated!