Python-Django多个数据库-TypeError:allow_migrate()缺少1个必需的位置参数:'app_label'错误

时间:2018-07-11 06:59:28

标签: python django django-rest-framework

因此,我正在django框架设置中创建多个数据库分配。我正在关注https://docs.djangoproject.com/en/2.0/topics/db/multi-db/

但是当我尝试运行它时,我的数据库路由器遇到了一些错误。

TypeError: allow_migrate() missing one required positional argument : 'app_label'

这些错误发生在我的AuthRouter.py和PrimaryReplicaRouter.py

下面是我的AuthRouter.py和PrimaryReplicaRouter.py代码

AuthRouter.py代码

class AuthRouter:

def db_for_read(self, model, **hints):
    if model._meta.app_label == 'auth':
        return 'auth_db'
    return None

def db_for_write(self, model, **hints):
    if model._meta.app_label == 'auth':
        return 'auth_db'
    return None

def allow_relation(self, obj1, obj2, **hints):
    if obj1._meta.app_label == 'auth' or \
       obj2._meta.app_label == 'auth':
       return True
    return None

def allow_migrate(self, db, app_label, model_name=None, **hints):
    if app_label == 'auth':
        return db == 'auth_db'
    return None

PrimaryReplicaRouter.py代码

import random

class PrimaryReplicaRouter:

def db_for_read(self, model, **hints):
    return random.choice(['db1', 'db2'])

def db_for_write(self, model, **hints):
    return 'primarydb'

def allow_relation(self, obj1, obj2, **hints):
    db_list = ('primarydb', 'db1', 'db2')
    if obj1._state.db in db_list and obj2._state.db in db_list:
        return True
    return None

def allow_migrate(self, db, app_label, model_name=None, **hints):
    """
    All non-auth models end up in this pool.
    """
    return True

这里有人也遇到这种问题吗?

谢谢大家的帮助。 :)

1 个答案:

答案 0 :(得分:0)

尝试一下:

@staticmethod
def allow_migrate(db, app_label, model_name=None, **hints):
    """
    All non-auth models end up in this pool.
    """
    return True

您还应该具有装饰器@staticmethod用于其他功能。