您正在尝试在没有默认值的情况下向用户添加不可为空的字段'password'



我正在Docker环境中使用以下Docker YAML文件构建Django项目:

django_3.2.4:
build:
context: ./ImageBuild/
dockerfile: Dockerfile_Build_Django
container_name: Django_3.2.4
depends_on: 
- Postgres_13.2
environment:
- DJANGO_SUPERUSER_USERNAME=admin
- DJANGO_SUPERUSER_PASSWORD=123456
- DJANGO_SUPERUSER_EMAIL=admin@abc.com
networks:
external_net:
ipv4_address: 192.168.100.22
restart: always
volumes: 
- ./IoTSite/:/usr/src/app/
- /etc/localtime:/etc/localtime:ro
command: sh -c "python manage.py makemigrations && python manage.py migrate && python manage.py createsuperuser --noinput --no-input && rsync --daemon && gunicorn"

我创建了一个自定义用户模型,如下所示:(在settings.py中添加了AUTH_User_Model='IoTSite.User'(

class User(AbstractUser):
UserID = models.CharField('username:',help_text='required', max_length=20, primary_key=True,unique=True, blank=False)
USERNAME_FIELD = 'UserID'
First_Name = models.CharField('first_name:',max_length = 2)
Last_Name = models.CharField('last_name:',max_length = 2)
Date_Created = models.DateTimeField(auto_now_add=True, editable=False)  #timestamps for creation, hidden from user input
Date_Birth = models.DateField('Birthday:')
Gender_Choice = [
('Male','Male'),
('Female','Female'),
]
Gender = models.CharField('Gender:',choices=Gender_Choice, max_length=1)

当我通过命令docker-compose up运行容器时,它失败了,并出现以下错误:(容器一直在重新启动,我没有机会键入我的选择(

2021-06-15T03:17:32.964281763Z WARNINGS:
2021-06-15T03:17:32.964284245Z IoTSite.Category: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
2021-06-15T03:17:32.964287706Z  HINT: Configure the DEFAULT_AUTO_FIELD setting or the IoTSiteConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
2021-06-15T03:17:32.964300827Z IoTSite.Tag: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
2021-06-15T03:17:32.964303505Z  HINT: Configure the DEFAULT_AUTO_FIELD setting or the IoTSiteConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
2021-06-15T03:17:32.964252008Z You are trying to add a non-nullable field 'password' to user without a default; we can't do that (the database needs something to populate existing rows).
2021-06-15T03:17:32.964327053Z Please select a fix:
2021-06-15T03:17:32.964329519Z  1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
2021-06-15T03:17:32.964331734Z  2) Quit, and let me add a default in models.py
2021-06-15T03:17:32.966604773Z Traceback (most recent call last):
2021-06-15T03:17:32.966623599Z   File "manage.py", line 21, in <module>
2021-06-15T03:17:32.966628470Z     main()
2021-06-15T03:17:32.966632261Z   File "manage.py", line 17, in main
2021-06-15T03:17:32.966635587Z     execute_from_command_line(sys.argv)
2021-06-15T03:17:32.966645274Z   File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
2021-06-15T03:17:32.966648823Z     utility.execute()
2021-06-15T03:17:32.966651673Z   File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 413, in execute
2021-06-15T03:17:32.966654967Z     self.fetch_command(subcommand).run_from_argv(self.argv)
2021-06-15T03:17:32.966658158Z   File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 354, in run_from_argv
2021-06-15T03:17:32.966661881Z     self.execute(*args, **cmd_options)
2021-06-15T03:17:32.966665276Z   File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 398, in execute
2021-06-15T03:17:32.966668279Z     output = self.handle(*args, **options)
2021-06-15T03:17:32.966670415Z   File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 89, in wrapped
2021-06-15T03:17:32.966672767Z     res = handle_func(*args, **kwargs)
2021-06-15T03:17:32.966675010Z   File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/makemigrations.py", line 176, in handle
2021-06-15T03:17:32.966677391Z     migration_name=self.migration_name,
2021-06-15T03:17:32.966679496Z   File "/usr/local/lib/python3.7/site-packages/django/db/migrations/autodetector.py", line 41, in changes
2021-06-15T03:17:32.966681780Z     changes = self._detect_changes(convert_apps, graph)
2021-06-15T03:17:32.966683889Z   File "/usr/local/lib/python3.7/site-packages/django/db/migrations/autodetector.py", line 183, in _detect_changes
2021-06-15T03:17:32.966686197Z     self.generate_added_fields()
2021-06-15T03:17:32.966688301Z   File "/usr/local/lib/python3.7/site-packages/django/db/migrations/autodetector.py", line 859, in generate_added_fields
2021-06-15T03:17:32.966690981Z     self._generate_added_field(app_label, model_name, field_name)
2021-06-15T03:17:32.966693174Z   File "/usr/local/lib/python3.7/site-packages/django/db/migrations/autodetector.py", line 880, in _generate_added_field
2021-06-15T03:17:32.966695472Z     field.default = self.questioner.ask_not_null_addition(field_name, model_name)
2021-06-15T03:17:32.966697647Z   File "/usr/local/lib/python3.7/site-packages/django/db/migrations/questioner.py", line 153, in ask_not_null_addition
2021-06-15T03:17:32.966708157Z     "Quit, and let me add a default in models.py",
2021-06-15T03:17:32.966710562Z   File "/usr/local/lib/python3.7/site-packages/django/db/migrations/questioner.py", line 98, in _choice_input
2021-06-15T03:17:32.966713409Z     result = input("Select an option: ")
2021-06-15T03:17:32.966715649Z EOFError: EOF when reading a line
2021-06-15T03:17:33.178780079Z Select an option: 2021-06-15T03:17:35.570587292Z You are trying to add a non-nullable field 'password' to user without a default; we can't do that (the database needs something to populate existing rows).

Q1:请问我应该如何修复上述错误

编辑:我查看了源代码,django.contribs.auth.base_user.py

class AbstractBaseUser(models.Model):
password = models.CharField(_('password'), max_length=128)
last_login = models.DateTimeField(_('last login'), blank=True, null=True)
is_active = True
REQUIRED_FIELDS = []

和django.contribs.auth.models.py

class AbstractUser(AbstractBaseUser, PermissionsMixin):
EMAIL_FIELD = 'email'
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']

Q2:在makemigration期间,在DB中创建表(还不是真正的用户(时,为什么需要字段password的默认值

将自定义Model类名从class User(AbstractUser):更改为class Employee(AbstractUser):可以解决问题。我不知道为什么,也许是因为这一更改避免了Model类名的重复(还有另一个django内置的模型类也被命名为"User(

//django.contrib.auth.models.py
class User(AbstractUser):

最新更新