You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django自定义模型类迁移报错:AttributeError缺少REQUIRED_FIELDS属性

Django模型迁移时出现AttributeError问题

参照在线教程编写了如下Django模型类代码,本应可正常执行迁移操作,但运行makemigrations命令时,反复出现AttributeError,提示'type object 'Foo' has no attribute 'REQUIRED_FIELDS'或'USERNAME_FIELD'。不理解为何需要添加这些属性,而教程中的代码无需它们即可正常运行。

编写的模型代码

from django.db import models

class Foo(models.Model):
    pass

执行的命令及报错信息

执行命令:

python3 manage.py makemigrations things

报错信息:

Traceback (most recent call last):
  File "/home/kofkbuntu/uni/year2/5CCS2SEG/week2/ThingsApp1/things-1-jnile/manage.py", line 22, in <module>
    main()
  File "/home/kofkbuntu/uni/year2/5CCS2SEG/week2/ThingsApp1/things-1-jnile/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/kofkbuntu/uni/year2/5CCS2SEG/week2/ThingsApp1/things-1-jnile/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/home/kofkbuntu/uni/year2/5CCS2SEG/week2/ThingsApp1/things-1-jnile/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/kofkbuntu/uni/year2/5CCS2SEG/week2/ThingsApp1/things-1-jnile/venv/lib/python3.10/site-packages/django/core/management/base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/kofkbuntu/uni/year2/5CCS2SEG/week2/ThingsApp1/things-1-jnile/venv/lib/python3.10/site-packages/django/core/management/base.py", line 443, in execute
    self.check()
  File "/home/kofkbuntu/uni/year2/5CCS2SEG/week2/ThingsApp1/things-1-jnile/venv/lib/python3.10/site-packages/django/core/management/base.py", line 475, in check
    all_issues = checks.run_checks(
  File "/home/kofkbuntu/uni/year2/5CCS2SEG/week2/ThingsApp1/things-1-jnile/venv/lib/python3.10/site-packages/django/core/checks/registry.py", line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "/home/kofkbuntu/uni/year2/5CCS2SEG/week2/ThingsApp1/things-1-jnile/venv/lib/python3.10/site-packages/django/contrib/auth/checks.py", line 29, in check_user_model
    if not isinstance(cls.REQUIRED_FIELDS, (list, tuple)):
AttributeError: type object 'Foo' has no attribute 'REQUIRED_FIELDS'

解决方案

问题出在你可能将Foo配置成了Django的认证用户模型,但普通的models.Model并不包含认证模型所需的核心属性。Django的认证系统要求自定义用户模型必须继承AbstractBaseUser或AbstractUser,这类基类内置了REQUIRED_FIELDS、USERNAME_FIELD等必要属性。

有两种处理方式:

  1. 移除认证模型配置:如果Foo只是普通数据模型,不是用来做用户认证的,打开项目的settings.py,找到AUTH_USER_MODEL配置项,确认是否设置为了your_app_name.Foo,删除该配置后即可正常执行迁移。
  2. 调整模型继承关系:如果确实需要把Foo作为自定义认证模型,修改代码使其继承AbstractBaseUser:
from django.contrib.auth.models import AbstractBaseUser

class Foo(AbstractBaseUser):
    pass

注意:仅继承AbstractBaseUser还不够,后续还需要配置对应的用户管理器(UserManager),并明确设置USERNAME_FIELD等属性,才能让认证系统正常工作。

内容的提问来源于stack exchange,提问作者Jaidev Nileshkumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 22:25:17