database router raises TypeError: expecting router class instance as first argument

106 views
Skip to first unread message

Chris Ghormley

unread,
Mar 20, 2012, 10:55:30 PM3/20/12
to Django users
I'm having trouble with database routers in a Django 1.3.1 project.
I'm by no means a Django or Python expert, but I've been using Django
for a few years now. When I enable any kind of router in my project,
it throws a TypeError like the following:

"unbound method db_for_read() must be called with DBRouter instance as
first argument (got ModelBase instance instead)"

I've tried a few different examples based on the Django documentation,
but it seems like I am missing something basic here.

Does anybody have an idea what I've done wrong? Please let me know if
you need more information.

Here's some background:

The new database is used by a new app with a model in the app's
models.py. Using the shell I've proved that the database and model are
OK.

The routers.py is in the new app. The settings file imports the router
class, and includes the following line:

DATABASE_ROUTERS = [ DBRouter, ]

Here's the basic layout:
project
settings.py
app1 (uses database 1)
models.py
app2 (uses database 2)
models.py
routers.py


Chris Ghormley

unread,
Mar 21, 2012, 1:23:58 AM3/21/12
to django...@googlegroups.com
The router I was using was based on the router class in the Django multi-db docs, so I assumed that the syntax would work.

They write:

class MyAppRouter(object):
    """A router to control all database operations on models in
    the myapp application"""

    def db_for_read(self, model, **hints):
        "Point all operations on myapp models to 'other'"
        if model._meta.app_label == 'myapp':
            return 'other'
        return None

But then the way they are invoking the method there is no instance of MyAppRouter, so I worked around this with the following modification:

@staticmethod    
def
db_for_read(model, **hints):

Is there a reason the example looks like that, or is it Just Wrong?

Tom Evans

unread,
Mar 21, 2012, 6:50:53 AM3/21/12
to django...@googlegroups.com
On Wed, Mar 21, 2012 at 2:55 AM, Chris Ghormley
<chris.g...@gmail.com> wrote:
> I'm having trouble with database routers in a Django 1.3.1 project.
> I'm by no means a Django or Python expert, but I've been using Django
> for a few years now. When I enable any kind of router in my project,
> it throws a TypeError like the following:
>
> "unbound method db_for_read() must be called with DBRouter instance as
> first argument (got ModelBase instance instead)"
>
> I've tried a few different examples based on the Django documentation,
> but it seems like I am missing something basic here.
>
> Does anybody have an idea what I've done wrong? Please let me know if
> you need more information.
>
> Here's some background:
>
> The new database is used by a new app with a model in the app's
> models.py. Using the shell I've proved that the database and model are
> OK.
>
> The routers.py is in the new app. The settings file imports the router
> class, and includes the following line:
>
> DATABASE_ROUTERS = [ DBRouter, ]
>

https://docs.djangoproject.com/en/1.3/topics/db/multi-db/#topics-db-multi-db-routing

"""
Then, in your settings file, add the following (substituting path.to.
with the actual python path to the module where you define the
routers):

DATABASE_ROUTERS = ['path.to.MyAppRouter', 'path.to.MasterSlaveRouter']
"""

DATABASE_ROUTERS is supposed to be a list of strings, each string
denoting the python path and class name of a DB router class. You are
setting it to a list of klasses.

Django will take care of instantiating instances of the DB router
classes as needed.

Cheers

Tom

Chris Ghormley

unread,
Mar 21, 2012, 12:05:31 PM3/21/12
to django...@googlegroups.com

Thanks Tom. I was too busy to think clearly yesterday so when I tested with quotes I didn't include the path to my routers module.

In my experience the Django docs are good, just a little DRY - they only point out the obvious *once*.

Chris
Reply all
Reply to author
Forward
0 new messages