问题描述
我正在以最基本的方式使用自定义用户模型,如。但是,我不知道如何正确地使这项工作与南方。我在进行初始模式迁移时遇到问题,这里发生了什么:
I'm trying to use a custom user model in the most basic way, extending AbstractUser as described at Extending Django’s default User. But, I'm not sure how to properly make this work with South. I'm running into a problem when doing an initial schema migration with Here's what happening:
-
我有我的扩展用户在 c 中,在我的课程比赛中,在 settings.INSTALLED_APPS 。
要执行初始 syncdb ,锦标赛应用程序必须启用。否则我收到此错误:
To perform an initial syncdb, the tournaments app must be enabled. Otherwise I get this error:
$ ./manage.py syncdb CommandError: One or more models did not validate: auth.user: Model has been swapped out for 'tournaments.Player' which has not been installed or is abstract. admin.logentry: 'user' has a relation with model tournaments.Player, which has either not been installed or is abstract.
所以,我启用比赛应用程序,具有我的 Player (自定义用户)模型。但是,在初始迁移之后:
So, I enable the tournaments app that has my Player (custom user) model. But then, upon the initial migration:
$ ./manage.py schemamigration tournaments --initial ... $ ./manage.py migrate tournaments Running migrations for tournaments: - Migrating forwards to 0001_initial. > tournaments:0001_initial FATAL ERROR - The following SQL query failed: CREATE TABLE "tournaments_player" ("id" serial NOT NULL PRIMARY KEY, "password" varchar(128) NOT NULL, "last_login" timestamp with time zone NOT NULL, "is_superuser" boolean NOT NULL, "username" varchar(30) NOT NULL UNIQUE, "first_name" varchar(30) NOT NULL, "last_name" varchar(30) NOT NULL, "email" varchar(75) NOT NULL, "is_staff" boolean NOT NULL, "is_active" boolean NOT NULL, "date_joined" timestamp with time zone NOT NULL, "bio" text NOT NULL); The error was: relation "tournaments_player" already exists Error in migration: tournaments:0001_initial DatabaseError: relation "tournaments_player" already exists
如果我跳过 syncdb ,我收到这个错误,因为 syncdb 是必须引导南:
If I skip the syncdb, I get this error, because syncdb is necessary to bootstrap South:
$ ./manage.py migrate tournaments DatabaseError: relation "south_migrationhistory" does not exist LINE 1: ...gration", "south_migrationhistory"."applied" FROM "south_mig...
所以,看来我在这里有一个鸡/鸡的情况:我不能 syncdb 没有我的用户模型。但是,如果我的用户型号为 syncdb ,则无法执行初始迁移!
So, it appears that I've got a chicken/egg situation here: I can't syncdb without my user model. But, if I syncdb with my user model, I can't perform an initial migration!
解决这个问题的最好方法是什么?我有一些想法,比如运行初始的 syncdb 而不使用 django.contrib.auth 和 django.contrib.admin 启用,或运行初始 syncdb ,而不启用 south 然后转换应用程序。两个选项似乎都是黑客和奇怪的。
What is the best way to get around this? I've got some ideas, like running the initial syncdb without django.contrib.auth and django.contrib.admin enabled, or running the initial syncdb without south enabled and then converting the app. Both options seem hackey and strange.
推荐答案
尝试运行 ./ manage.py migrate tournament --fake 0001 。这将在南移民历史中添加一个条目,说明初始迁移已经运行,而不实际执行SQL。或者,您可以在自定义用户对象上设置 class Meta:managed = False ,以告诉django不要syncdb该模型。
Try running ./manage.py migrate tournaments --fake 0001. This will add an entry into the south migration history saying that the initial migration has run, without actually executing the SQL. Alternatively, you can set class Meta: managed = False on the custom user object to tell django not to ever syncdb that model.
这篇关于自定义用户模型和南方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!