python data_error ai_generated true

django.db.utils.IntegrityError: NOT NULL constraint failed: auth_user.username

ID: python/django-auth-user-creation-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2025-07-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.2 active
4.0 active
5.0 active

Root Cause

创建用户时未提供用户名

generic

中文

Django 用户模型要求用户名不能为空

Workarounds

  1. 100% success 在创建用户时提供用户名
    User.objects.create_user(username='john', email='[email protected]', password='pass')
  2. 95% success 使用 create() 方法并确保提供所有必填字段
    user = User(username='john', email='[email protected]'); user.set_password('pass'); user.save()

Dead Ends

Common approaches that don't work:

  1. 设置 username 为 NULL 100% fail

    数据库约束不允许 NULL

  2. 使用 email 作为用户名 50% fail

    需要自定义用户模型