Django question
I have 2 models (Author
and Book
) related by a foreign key and I want to query all Author
s and display them with their newest Book
, here is a sample of my code:
# models.py
class Author(models.Model):
name = models.CharField(max_length=200)
class Book(models.Model):
title = models.CharField(max_length=200)
published = models.DateField()
author = models.ForeignKey(Author, on_delete=models.CASCADE)
# views.py
def index(request):
authors = [
(author, author.book_set.order_by('-published').first())
for author in Author.objects.all()
]
return render(request, 'index.html', {'authors': authors})
{# index.html #}
{% extends 'base.html' %}
{% block content %}
{% for author, newest_book in authors %}
<p>
{{ newest_book.title }} by {{ author.name }}. Published on {{ newest_book.published }}
</p>
{% endfor %}
{% endblock %}
However, I don’t like this since this approach just queries the database countless times…
Also I would like to be able to add some filters for example to only display Author
s with their latest Book
if the latest Book
was published within the last 30 days (I would need an inner query and a filter on the many side)…