2015-07-02 72 views
3

我想添加一个字段Django用户。所以我检查这个文件:https://docs.djangoproject.com/en/1.8/topics/auth/customizing/Django AbstractUser migrate无法解析

我决定用AbstractUser来解决我的问题。

地址:

class MyUser(AbstractUser): 
    nickname = models.CharField(max_length=64, blank=False) 

muse.models

并将AUTH_USER_MODEL = "muse.MyUser"添加到settings.py文件。

但是,当我migrate总是有错误:

Synchronize unmigrated apps: staticfiles, messages 
    Apply all migrations: admin, contenttypes, muse, auth, sessions 
Synchronizing apps without migrations: 
    Creating tables... 
    Running deferred SQL... 
    Installing custom SQL... 
Running migrations: 
    Rendering model states... DONE 
    Applying admin.0001_initial...Traceback (most recent call last): 
    File "manage.py", line 10, in <module> 
    execute_from_command_line(sys.argv) 
    File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line 
    utility.execute() 
    File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 330, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv 
    self.execute(*args, **cmd_options) 
    File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 441, in execute 
    output = self.handle(*args, **options) 
    File "/Library/Python/2.7/site-packages/django/core/management/commands/migrate.py", line 221, in handle 
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial) 
    File "/Library/Python/2.7/site-packages/django/db/migrations/executor.py", line 110, in migrate 
    self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial) 
    File "/Library/Python/2.7/site-packages/django/db/migrations/executor.py", line 147, in apply_migration 
    state = migration.apply(state, schema_editor) 
    File "/Library/Python/2.7/site-packages/django/db/migrations/migration.py", line 115, in apply 
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state) 
    File "/Library/Python/2.7/site-packages/django/db/migrations/operations/models.py", line 59, in database_forwards 
    schema_editor.create_model(model) 
    File "/Library/Python/2.7/site-packages/django/db/backends/base/schema.py", line 232, in create_model 
    definition, extra_params = self.column_sql(model, field) 
    File "/Library/Python/2.7/site-packages/django/db/backends/base/schema.py", line 131, in column_sql 
    db_params = field.db_parameters(connection=self.connection) 
    File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py", line 1989, in db_parameters 
    return {"type": self.db_type(connection), "check": []} 
    File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py", line 1980, in db_type 
    rel_field = self.related_field 
    File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py", line 1886, in related_field 
    return self.foreign_related_fields[0] 
    File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py", line 1620, in foreign_related_fields 
    return tuple(rhs_field for lhs_field, rhs_field in self.related_fields) 
    File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py", line 1607, in related_fields 
    self._related_fields = self.resolve_related_fields() 
    File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py", line 1592, in resolve_related_fields 
    raise ValueError('Related model %r cannot be resolved' % self.rel.to) 
ValueError: Related model 'muse.MyUser' cannot be resolved 

有什么不对?

+3

如果你删除你的迁移(或'mv muse/migrations muse/migrations-BU'),然后重新运行'./manage.py makemigrations, ./manage.py migrate'? –

+0

哦..问题已修复,非常感谢。 –

+2

关闭问题... – madzohan

回答

0

在我的情况下,一个棘手的解决方法是可用的,我不建议它,因为我的情况是如此特别。我有自己的数据库管理,并且由于某种原因将Django查询构建器放在一边(这是一个大学项目,它不被允许)。我手动操作数据库结构,并拥有自己的用户模型和身份验证后端。

解决方法是创建迁移,然后手动删除其操作。创建迁移之后,我必须禁用AUTH_USER_MODEL,迁移然后启用该设置以使认证系统正常工作。

希望它有帮助。