Get all books by an author

Is there a way to get all books by an author in a way in this existing query itself ?

def view(request, author_id):

    query = Q(is_trash='0')

    # TODO : need to get only books by author = author_id
    books = Books.objects.all().filter(query).order_by('-published')

    for book in books:
        # Add some more details

    return render(request, 'author/view.html', { 'books' : books })


books = Books.objects.filter(author_id=author_id, is_trash='0').order_by('-published')

the best bet ?

If author is foreign key in the Books model than instead of this

use double underscore for the field like author__id than equals to the value i.e author_id

Books.objects.filter(author__id=author_id, is_trash='0')

Note: If you look at the queries generated by those statements, you’ll see that they are the same. There is no functional difference between them for the case as written. In those situations where there is a difference in the query, the first version (author_id=author_id) is actually better, because it prevents the possibility of an extraneous query being made. As a result, his original query is actually the better solution.