Django-设置AUTH_USER_MODEL

时间:2018-12-25 07:25:45

标签: python django

我创建了一个名为Backend的Django项目,并在其中创建了一个名为Tool的应用。

现在,我通过继承 User模型创建了AbstractUser模型,并使用此模型创建了2个帐户

class User(AbstractUser):
    is_student = models.BooleanField(default=False)
    is_mentor  = models.BooleanField(default=False)

class Student(models.Model):
    user       = models.OneToOneField(User, on_delete=models.CASCADE)
    college    = models.CharField(max_length=100)
    ...

class Mentor(models.Model):
    user       = models.OneToOneField(User, on_delete=models.CASCADE)
    ...

我已通过以下方式更新settings.py文件夹中的Backend

AUTH_USER_MODEL = 'Tool.User'

makemigrationsmigrate之后,出现以下错误:

  

ValueError:无法解析相关的模型'Tool.User'


跟踪:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\Sreekar Mouli\Documents\Mini Project\Backend\venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\Sreekar Mouli\Documents\Mini Project\Backend\venv\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Sreekar Mouli\Documents\Mini Project\Backend\venv\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Sreekar Mouli\Documents\Mini Project\Backend\venv\lib\site-packages\django\core\management\base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "C:\Users\Sreekar Mouli\Documents\Mini Project\Backend\venv\lib\site-packages\django\core\management\base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\Sreekar Mouli\Documents\Mini Project\Backend\venv\lib\site-packages\django\core\management\commands\migrate.py", line 203, in handle
    fake_initial=fake_initial,
  File "C:\Users\Sreekar Mouli\Documents\Mini Project\Backend\venv\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\Users\Sreekar Mouli\Documents\Mini Project\Backend\venv\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\Users\Sreekar Mouli\Documents\Mini Project\Backend\venv\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\Sreekar Mouli\Documents\Mini Project\Backend\venv\lib\site-packages\django\db\migrations\migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Users\Sreekar Mouli\Documents\Mini Project\Backend\venv\lib\site-packages\django\db\migrations\operations\models.py", line 91, in database_forwards
    schema_editor.create_model(model)
  File "C:\Users\Sreekar Mouli\Documents\Mini Project\Backend\venv\lib\site-packages\django\db\backends\base\schema.py", line 263, in create_model
    definition, extra_params = self.column_sql(model, field)
  File "C:\Users\Sreekar Mouli\Documents\Mini Project\Backend\venv\lib\site-packages\django\db\backends\base\schema.py", line 157, in column_sql
    db_params = field.db_parameters(connection=self.connection)
  File "C:\Users\Sreekar Mouli\Documents\Mini Project\Backend\venv\lib\site-packages\django\db\models\fields\related.py", line 966, in db_parameters
    return {"type": self.db_type(connection), "check": self.db_check(connection)}
  File "C:\Users\Sreekar Mouli\Documents\Mini Project\Backend\venv\lib\site-packages\django\db\models\fields\related.py", line 963, in db_type
    return self.target_field.rel_db_type(connection=connection)
  File "C:\Users\Sreekar Mouli\Documents\Mini Project\Backend\venv\lib\site-packages\django\db\models\fields\related.py", line 878, in target_field
    return self.foreign_related_fields[0]
  File "C:\Users\Sreekar Mouli\Documents\Mini Project\Backend\venv\lib\site-packages\django\db\models\fields\related.py", line 632, in foreign_related_fields
    return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
  File "C:\Users\Sreekar Mouli\Documents\Mini Project\Backend\venv\lib\site-packages\django\db\models\fields\related.py", line 619, in related_fields
    self._related_fields = self.resolve_related_fields()
  File "C:\Users\Sreekar Mouli\Documents\Mini Project\Backend\venv\lib\site-packages\django\db\models\fields\related.py", line 604, in resolve_related_fields
    raise ValueError('Related model %r cannot be resolved' % self.remote_field.model)

3 个答案:

答案 0 :(得分:3)

我已经清除了以前的所有迁移,删除了数据库并重新创建。当我运行makemigrationsmigrate时,效果很好。

似乎以前的迁移存在一些问题。但是,它正在显示此错误,这令人感到困惑和困惑。

  

ValueError:无法解析相关的模型'Tool.User'

答案 1 :(得分:0)

您的应用名称是什么?字母大小很重要

应用“工具”,模型“用户”

settings.py

INSTALLED_APPS = (
    ...
    "Tool",
)
AUTH_USER_MODEL = 'Tool.User'

如果要将用户导入到其他文件,最好采用这种方式

views.py

from django.contrib.auth import get_user_model
User = get_user_model()

并尝试进行新的迁移。如果您不想添加部门。

答案 2 :(得分:0)

我认为您在更改USER model in middle of project时遇到问题。此更改无法自动完成,需要手动完成。根据{{​​3}},您可以按照以下步骤手动迁移用户模型:

  1. 创建一个与auth.User相同的自定义用户模型,将其命名为User(许多表保持相同的名称)并设置db_table='auth_user'(因此它使用相同的表)
  2. 放弃所有迁移
  3. 重新创建一组新的迁移
  4. 备份数据库
  5. 截断django_migrations表
  6. 假套用新的迁移集
  7. 取消db_table的设置,对自定义模型进行其他更改,生成迁移,然后应用

如果您愿意删除数据库,则可以遵循@SreekarMouli的回答。