# Defining Models Database in class based view

**URL:** <https://forum.djangoproject.com/t/defining-models-database-in-class-based-view/18838>\
**Category:** Forms & APIs\
**Created:** [February 14, 2023, 1:53pm UTC](https://forum.djangoproject.com/t/defining-models-database-in-class-based-view/18838 "2023-02-14T13:53:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Timmy-Tenders](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/timmy-tenders/32/10899_2.png) [@Timmy-Tenders](https://forum.djangoproject.com/u/Timmy-Tenders)\
**Post date:** [February 14, 2023, 1:53pm UTC](https://forum.djangoproject.com/t/defining-models-database-in-class-based-view/18838/1 "2023-02-14T13:53:48Z")

</div>

Hello,

The following is a sample from the django website documentation on class based views. The part of interest to me was defining the models database. It seems that in this example this is in the default database. In my application I usually have to use .using(‘database\_name’) to connect my model to the correct database. How would this be done in this case?(or is a router a requirement)

Thanks,

```auto
from django.http import HttpResponse
from django.views.generic import ListView
from books.models import Book

class BookListView(ListView):
    model = Book

    def head(self, *args, **kwargs):
        last_book = self.get_queryset().latest('publication_date')
        response = HttpResponse(
            # RFC 1123 date format.
            headers={'Last-Modified': last_book.publication_date.strftime('%a, %d %b %Y %H:%M:%S GMT')},
        )
        return response

```

> **[Django](https://docs.djangoproject.com/en/4.1/topics/class-based-views/)**
>
> The web framework for perfectionists with deadlines.

---

<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:** [February 14, 2023, 3:09pm UTC](https://forum.djangoproject.com/t/defining-models-database-in-class-based-view/18838/2 "2023-02-14T15:09:00Z")

</div>

You’ll be looking to override the `get_queryset` method.

See [Built-in class-based generic views | Django documentation | Django](https://docs.djangoproject.com/en/4.1/topics/class-based-views/generic-display/#dynamic-filtering)  
and [Multiple object mixins | Django documentation | Django](https://docs.djangoproject.com/en/4.1/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectMixin.get_queryset)
