InvalidBasesError:无法解析[<ModelState:'users.GroupProxy'>]的基

2024-05-23 18:22:03 发布

您现在位置:Python中文网/ 问答频道 /正文

当我运行测试时,在数据库初始化期间出现此错误:

django.db.migrations.state.InvalidBasesError: Cannot resolve bases for [<ModelState: 'users.GroupProxy'>]
This can happen if you are inheriting models from an app with migrations (e.g. contrib.auth)

我为contrib.auth Group model创建了此代理,以便将其放在django admin中的应用程序中:

class GroupProxy(Group):
    class Meta:
        proxy = True
        verbose_name = Group._meta.verbose_name
        verbose_name_plural = Group._meta.verbose_name_plural

那么我能做些什么来解决这个问题呢?


Tags: djangonameauth数据库verbosedbmigrations错误
3条回答

我遇到了这个问题,由于注释模型并不是一个真正的解决方案,我发现将未记录的auto_created = True设置为元类将使Django忽略它。

class GroupProxy(Group):

    class Meta:
        proxy = True
        auto_created = True

简单地在应用程序的根目录创建一个migrations目录(在您的例子中是users/migrations/)并添加一个空的__init__.py文件就可以解决您的问题。至少在我犯同样的错误的时候对我来说是这样。

但是你最好为你的应用程序运行makemigrations,如 @上面是zenofewords。这将为您创建目录并为代理模型生成迁移。

Why does Django create migration files for proxy models?

您的测试正在寻找这些迁移,但没有找到它们。

在这件事上做了很多调查之后,我唯一能做的就是

comment out the offending apps, run migrations, then add them in again.

只是权宜之计,但希望能帮上忙。

相关问题 更多 >