1

我正在尝试扩展默认的 Django User 模型并在我的authentication应用程序中添加自定义字段,但是当我尝试运行py manage.py migrate它时提示错误,说它找不到'authentication.User模型。

顺便说一句,我正在使用 Djoser 进行身份验证。

我收到此错误:

>py manage.py migrate

Operations to perform:
  Apply all migrations: admin, auth, authentication, authtoken, contenttypes, sessions
Running migrations:
  Applying authentication.0001_initial...Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "D:\DJANGO-PROJECTS\chat_app\chat_app_env\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "D:\DJANGO-PROJECTS\chat_app\chat_app_env\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:\DJANGO-PROJECTS\chat_app\chat_app_env\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "D:\DJANGO-PROJECTS\chat_app\chat_app_env\lib\site-packages\django\core\management\base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "D:\DJANGO-PROJECTS\chat_app\chat_app_env\lib\site-packages\django\core\management\base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "D:\DJANGO-PROJECTS\chat_app\chat_app_env\lib\site-packages\django\core\management\commands\migrate.py", line 231, in handle
    post_migrate_state = executor.migrate(
  File "D:\DJANGO-PROJECTS\chat_app\chat_app_env\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 "D:\DJANGO-PROJECTS\chat_app\chat_app_env\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 "D:\DJANGO-PROJECTS\chat_app\chat_app_env\lib\site-packages\django\db\migrations\executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File "D:\DJANGO-PROJECTS\chat_app\chat_app_env\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 "D:\DJANGO-PROJECTS\chat_app\chat_app_env\lib\site-packages\django\db\migrations\operations\models.py", line 92, in database_forwards
    schema_editor.create_model(model)
  File "D:\DJANGO-PROJECTS\chat_app\chat_app_env\lib\site-packages\django\db\backends\base\schema.py", line 322, in create_model
    sql, params = self.table_sql(model)
  File "D:\DJANGO-PROJECTS\chat_app\chat_app_env\lib\site-packages\django\db\backends\base\schema.py", line 159, in table_sql
    definition, extra_params = self.column_sql(model, field)
  File "D:\DJANGO-PROJECTS\chat_app\chat_app_env\lib\site-packages\django\db\backends\base\schema.py", line 212, in column_sql
    db_params = field.db_parameters(connection=self.connection)
  File "D:\DJANGO-PROJECTS\chat_app\chat_app_env\lib\site-packages\django\db\models\fields\related.py", line 971, in db_parameters
    return {"type": self.db_type(connection), "check": self.db_check(connection)}
  File "D:\DJANGO-PROJECTS\chat_app\chat_app_env\lib\site-packages\django\db\models\fields\related.py", line 968, in db_type
    return self.target_field.rel_db_type(connection=connection)
  File "D:\DJANGO-PROJECTS\chat_app\chat_app_env\lib\site-packages\django\db\models\fields\related.py", line 880, in target_field
    return self.foreign_related_fields[0]
  File "D:\DJANGO-PROJECTS\chat_app\chat_app_env\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 "D:\DJANGO-PROJECTS\chat_app\chat_app_env\lib\site-packages\django\db\models\fields\related.py", line 619, in related_fields
    self._related_fields = self.resolve_related_fields()
  File "D:\DJANGO-PROJECTS\chat_app\chat_app_env\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)
ValueError: Related model 'authentication.User' cannot be resolved

我尝试在我的数据库中删除迁移和 django_migrations 表,但没有任何改变。

这是我的代码:

设置.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'rest_framework.authtoken',
    'authentication',
    'djoser',
]

# REST FRAMEWORK
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.TokenAuthentication',
    ],
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ]
}

AUTH_USER_MODEL = 'authentication.User'

# DJOSER
DJOSER = {
    'CREATE_SESSION_ON_LOGIN': True,
    'USER_CREATE_PASSWORD_RETYPE':True,
    'SERIALIZERS':{
        'user_create': 'authentication.serializers.UserCreateSerializer',
        'user': 'authentication.serializers.UserCreateSerializer',
    }
    
}

模型.py:

from django.db import models
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    middle_name = models.CharField(null=True, max_length=100)
    REQUIRED_FIELDS = ['first_name', 'middle_name', 'last_name', 'password']
    USERNAME_FIELD = 'username'
    search_fields = ('username',)

    def get_username(self):
        return self.username

序列化程序.py

from djoser.serializers import UserCreateSerializer, UserSerializer
from rest_framework import serializers
from .models import User

class UserCreateSerializer(UserCreateSerializer):
    class Meta(UserSerializer.Meta):
        model = User
        fields = ('id', 'email', 'username', 'first_name', 'middle_name', 'last_name')

管理员.py:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import User

admin.site.register(User, UserAdmin)

我希望有人可以提供帮助,我是 Django 的新手,现在在这个错误中停留了 4 个小时。谢谢!

4

0 回答 0