我正在尝试以最基本的方式使用自定义用户模型,AbstractUser
如Extending Django's default User中所述进行扩展。但是,我不确定如何正确地使用 South。我在进行初始架构迁移时遇到了问题,情况如下:
我的扩展用户类 ,
Player
在我的类tournaments
中,在 中启用settings.INSTALLED_APPS
。要执行初始
syncdb
设置,tournaments
必须启用应用程序。否则我会收到此错误:$ ./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.
因此,我启用了
tournaments
具有我的Player
(自定义用户)模型的应用程序。但是,在初始迁移时:$ ./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
需要引导 South:
$ ./manage.py migrate tournaments
DatabaseError: relation "south_migrationhistory" does not exist
LINE 1: ...gration", "south_migrationhistory"."applied" FROM "south_mig...
所以,看来我在这里遇到了先有鸡还是先有蛋的情况:我不能syncdb
没有我的用户模型。但是,如果我syncdb
使用我的用户模型,我将无法执行初始迁移!
解决这个问题的最佳方法是什么?我有一些想法,比如在syncdb
不启用django.contrib.auth
和django.contrib.admin
启用的情况下运行初始设置,或者在syncdb
不south
启用的情况下运行初始设置然后转换应用程序。这两种选择看起来都很老套和奇怪。