# Get all books by an author

**URL:** <https://forum.djangoproject.com/t/get-all-books-by-an-author/19919>\
**Category:** Using the ORM\
**Created:** [March 31, 2023, 3:15am UTC](https://forum.djangoproject.com/t/get-all-books-by-an-author/19919 "2023-03-31T03:15:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anjanesh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anjanesh/32/4583_2.png) [@anjanesh](https://forum.djangoproject.com/u/anjanesh)\
**Post date:** [March 31, 2023, 3:15am UTC](https://forum.djangoproject.com/t/get-all-books-by-an-author/19919/1 "2023-03-31T03:15:42Z")

</div>

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

```auto
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 })

```

Is

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

```

the best bet ?

---

<div class="post-metadata">

**Author:** ![addwebsolution](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/addwebsolution/32/23762_2.png) [@addwebsolution](https://forum.djangoproject.com/u/addwebsolution)\
**Post date:** [April 3, 2023, 10:51am UTC](https://forum.djangoproject.com/t/get-all-books-by-an-author/19919/2 "2023-04-03T10:51:33Z")

</div>

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

> [@anjanesh](#):
>
> `Books.objects.filter(author_id=author_id, is_trash='0')`

use double underscore for the field like author\_\_id than equals to the value i.e author\_id

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

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 4, 2023, 2:40am UTC](https://forum.djangoproject.com/t/get-all-books-by-an-author/19919/3 "2023-04-04T02:40:17Z")

</div>

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.
