Let the model define the database with a custom router

26 views
Skip to first unread message

Stodge

unread,
Dec 15, 2017, 4:11:40 PM12/15/17
to Django users
I'm porting a website from Django 1.6.x to 1.11 and I'm hitting a problem with my custom router. Some models define a custom attribute defining which database it will reside in:

class MyModel(models.Model):
    DATABASE_NAME
= "foo"

This worked in our custom router in 1.6.x but not in 1.11. The router accesses it like:

        if hasattr(model,'DATABASE_NAME'):
           
return model.DATABASE_NAME == db

Inside the allow_migrate function of my router, the custom attribute DATABASE_NAME doesn't exist in the model in 1.11 but it was there in 1.6.x, if that makes sense.

Any suggestions for a different implementation? I want my models to define the physical database they are in.

gerno...@gmail.com

unread,
Dec 22, 2017, 12:51:56 PM12/22/17
to Django users
I have the same problem as Stodge.

What I know right know is that the method parameters of allow_migrate changed from def allow_migrate(self, db, model) to def allow_migrate(self, db, app_label, model_name=None, **hints). Since that change, you don't have any reference to a model class any more. Just strings parameters but no objects.

I've tried to gather a model class instance from that parameters in many ways, no luck so far. What you get sometimes is a class object in hints['models'], but too late to useful for our case.

If someone more skilled can help us out, very very welcome!
Cheers!

Reply all
Reply to author
Forward
0 new messages