Django创建超级用户

2024-10-04 05:25:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了模型用户 我添加了两行“is_superuser=models.BooleanField(default=False)” is_staff=models.BooleanField(default=False)“只是绕过问题并通过模型创建超级用户,而不是默认用户,但这不能解决我的问题。我需要创建一个默认的django super uesr

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

class User(AbstractUser):
    first_name= models.CharField(max_length=30)
    last_name= models.CharField(max_length=30)
    cin     = models.IntegerField()
    username= models.CharField(max_length=30,unique=True)   
    password= models.CharField(max_length=30)   
    codeQR  = models.CharField(max_length=30)
    poste   = models.CharField(max_length=30)
    image   = models.CharField(max_length=30)
    email   = models.EmailField()
    telephone= models.IntegerField()
    is_superuser=models.BooleanField(default=False)
    is_staff=models.BooleanField(default=False)

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = []


class pointage(models.Model):
    entre   = models.CharField(max_length=30)
    sortie  = models.CharField(max_length=30)
    retard  = models.CharField(max_length=30)
    absance = models.CharField(max_length=30)   
    user    = models.ManyToManyField(User)


class salaire(models.Model):
    mois      = models.IntegerField()
    heurs_base= models.FloatField()
    heurs_sup = models.FloatField()
    primes    = models.FloatField()  
    total     = models.FloatField()   
    user    = models.ManyToManyField(User)



但是我得到了以下错误

PS C:\py\pointage> py manage.py createsuperuser
Username: a
Password: 
Password (again):
Error: Your passwords didn't match.
Password: 
Password (again):
The password is too similar to the username.
This password is too short. It must contain at least 8 characters.
This password is too common.
Bypass password validation and create user anyway? [y/N]: y
Traceback (most recent call last):
  File "C:\Users\moezm\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\moezm\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\sqlite3\base.py", line 423, 
in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: NOT NULL constraint failed: employes_user.cin

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\py\pointage\manage.py", line 22, in <module>
    main()
  File "C:\py\pointage\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\moezm\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 419, 
in execute_from_command_line
    utility.execute()
  File "C:\Users\moezm\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 413, 
in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\moezm\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\moezm\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 79, in execute
    return super().execute(*args, **options)
  File "C:\Users\moezm\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "C:\Users\moezm\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 189, in handle
    self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
  File "C:\Users\moezm\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\models.py", line 163, in create_superuser
    return self._create_user(username, email, password, **extra_fields)
  File "C:\Users\moezm\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\models.py", line 146, in _create_user
    user.save(using=self._db)
  File "C:\Users\moezm\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\base_user.py", line 67, in 
save
    super().save(*args, **kwargs)
  File "C:\Users\moezm\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 726, in save   
    self.save_base(using=using, force_insert=force_insert,
  File "C:\Users\moezm\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 763, in save_base
    updated = self._save_table(
  File "C:\Users\moezm\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 868, in _save_table
    results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
  File "C:\Users\moezm\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 906, in _do_insert
    return manager._insert(
  File "C:\Users\moezm\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\moezm\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 1270, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "C:\Users\moezm\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\compiler.py", line 1416, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\moezm\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 98, in execute
    return super().execute(sql, params)
  File "C:\Users\moezm\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\moezm\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\moezm\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\moezm\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\utils.py", line 90, in __exit__      
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\moezm\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\moezm\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\sqlite3\base.py", line 423, 
in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: employes_user.cin

我不知道为什么会出现这个错误,也不知道如何修复它。你能帮我吗


Tags: djangoinpyexecutemodelsliblocalline
1条回答
网友
1楼 · 发布于 2024-10-04 05:25:29

之所以发生这种情况,是因为您使用AbstractUser覆盖了默认的User模型

下面是你要做的:

  1. 创建一个UserProfile模型,该模型具有OneToOneRelationshipUser模型
  2. 将以前指向User模型的所有模型更改为指向UserProfile模型

现在,当您创建超级用户时,您不会遇到任何问题

编辑:

您可以参考这个post,它显示了如何扩展默认的User模型

相关问题 更多 >