【问题标题】:Django - creating super userDjango - 创建超级用户
【发布时间】:2021-10-13 12:02:04
【问题描述】:

我创建了模型用户 我添加了这两行“ 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

我不知道为什么会出现这个错误或如何解决它,你能帮帮我吗!

【问题讨论】:

  • 那时你不应该使用AbstractUser
  • 我应该用什么!
  • 看来你必须写一个custom user manager 来添加对填充cin 字段的支持。

标签: python django


【解决方案1】:

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

这就是你要做的:

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

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

编辑:

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

【讨论】:

  • 你能指导我如何成为 django 的新手!
  • @moezmefteh 我添加了一个链接。检查。如果您仍有任何疑问,请咨询我。
  • 对不起我的朋友,但我仍然无法实现这个
猜你喜欢
  • 1970-01-01
  • 2012-12-13
  • 2021-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-22
  • 2015-02-17
  • 2021-08-19
相关资源
最近更新 更多