Error when getting all model names in django app

119 views Asked by At

I am using different dbs inside my django application and while db routing, i am trying to get all models using

all_models = dict([(name.lower(), cls) for name, cls in app.models.__dict__.items() \
 if isinstance(cls, type)])

While running migrations, i am getting error "name app is not defined".

This is my routers.py

from .models import *



  allmodels = dict([(name.lower(), cls) for name, cls in 
  app.models.__dict__.items() \
 if isinstance(cls, type)])

class MyDBRouter(object):

def db_for_read(self, model, **hints):
    """ reading model based on params """
    return getattr(model.params, 'db')

def db_for_write(self, model, **hints):
    """ writing model based on params """
    return getattr(model.params, 'db')

def allow_migrate(self, db, app_label, model_name = None, **hints):
    """ migrate to appropriate database per model """
    model = allmodels.get(model_name)
    return(model.params.db == db)

Settings.py https://pastebin.com/0XdpqxkJ

0

There are 0 answers