首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >值错误:无法分配"<ContentType: config>":当前数据库路由器阻止此关系

值错误:无法分配"<ContentType: config>":当前数据库路由器阻止此关系
EN

Stack Overflow用户
提问于 2021-07-12 17:04:07
回答 1查看 225关注 0票数 0

我在Django中使用多个数据库,并在settings.py中连接默认的SQLite和PostgreSQL db。

setting.py:

代码语言:javascript
运行
复制
DATABASE_ROUTERS = ['routers.db_routers.AppRouter']
DATABASE_APPS_MAPPING = {'product': 'postgres',}

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    },
    'postgres': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'product',
        'USER': 'postgres',
        'PASSWORD':'password',
        'HOST':'localhost'
    }

}

并且还将db_routers.py放在路由器文件夹中:

代码语言:javascript
运行
复制
class AppRouter:
    """
    A router to control all database operations on models in the
    product application.
    """
    def db_for_read(self, model, **hints):
        """
        Attempts to read user models go to postgres.
        """
        if model._meta.app_label == 'product':
            return 'postgres'
        return 'default'

    def db_for_write(self, model, **hints):
        """
        Attempts to write user models go to postgres.
        """
        if model._meta.app_label == 'product':
            return 'postgres'
        return 'default'

    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if a model in the user app is involved.
        """
        if obj1._meta.app_label == 'product' or \
           obj2._meta.app_label == 'product':
           return True
        return False

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """
        Make sure the auth app only appears in the 'product_db'
        database.
        """
        if app_label == 'product':
            return db == 'postgres'
        return None

这里,它是model.py:

代码语言:javascript
运行
复制
class Product(models.Model):
    name = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    weight = models.DecimalField(max_digits=10, decimal_places=2)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        app_label = 'product'
    
    def __str__(self):
        return self.name

我已经通过运行python3 manage.py makemigrations成功地创建了表,但是当我尝试使用python3 manage.py migrate --database=postgres进行迁移时,我得到了这个错误:raise ValueError(cannot assign "%r": the current database router prevents this relation.' % value) ValueError: Cannot assign "<ContentType: config>": the current database router prevents this relation.

EN

回答 1

Stack Overflow用户

发布于 2021-07-12 18:18:48

我已经通过在routers.py文件中添加elif条件更改了allow_relation方法,现在它可以正常工作了。

代码语言:javascript
运行
复制
def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if a model in the user app is involved.
        """
        if obj1._meta.app_label == 'product' or \
           obj2._meta.app_label == 'product':
           return True
        elif 'product' not in [obj1._meta.app_label, obj2._meta.app_label]:
            return True

        return False
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68344644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档