Django 中的项目:我无法在终端中创建超级用户来操作 Postgresql 中的数据

Posted

技术标签:

【中文标题】Django 中的项目:我无法在终端中创建超级用户来操作 Postgresql 中的数据【英文标题】:Project in Django: I can't create superuser in terminal to manipulate data in Postgresql 【发布时间】:2022-01-22 01:06:08 【问题描述】:

我正在使用 Django 做一个项目,我尝试将数据库从 SQLITE 更改为 Postqresql。当我尝试创建超级用户时,出现此错误。

追溯

File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\l\anaconda3\envs\myenv\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "C:\Users\l\anaconda3\envs\myenv\lib\site-packages\django\core\management\__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\l\anaconda3\envs\myenv\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\l\anaconda3\envs\myenv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 79, in execute
    return super().execute(*args, **options)
  File "C:\Users\l\anaconda3\envs\myenv\lib\site-packages\django\core\management\base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "C:\Users\l\anaconda3\envs\myenv\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\l\anaconda3\envs\myenv\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\l\anaconda3\envs\myenv\lib\site-packages\django\contrib\auth\models.py", line 146, in _create_user
    user.save(using=self._db)
  File "C:\Users\l\anaconda3\envs\myenv\lib\site-packages\django\contrib\auth\base_user.py", line 67, in save
    super().save(*args, **kwargs)
  File "C:\Users\l\anaconda3\envs\myenv\lib\site-packages\django\db\models\base.py", line 726, in save
    self.save_base(using=using, force_insert=force_insert,
  File "C:\Users\l\anaconda3\envs\myenv\lib\site-packages\django\db\models\base.py", line 774, in save_base
    post_save.send(
  File "C:\Users\l\anaconda3\envs\myenv\lib\site-packages\django\dispatch\dispatcher.py", line 180, in send
    return [
  File "C:\Users\l\anaconda3\envs\myenv\lib\site-packages\django\dispatch\dispatcher.py", line 181, in <listcomp>
    (receiver, receiver(signal=self, sender=sender, **named))
  File "C:\Users\l\Desktop\django-course\Django(02-09-21)\crm1\accounts\signals.py", line 7, in customer_profile
    group = Group.objects.get(name='customer')
  File "C:\Users\l\anaconda3\envs\myenv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\l\anaconda3\envs\myenv\lib\site-packages\django\db\models\query.py", line 435, in get
    raise self.model.DoesNotExist(
django.contrib.auth.models.DoesNotExist: Group matching query does not exist.

Settings.py

DATABASES = 
    'default': 
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'DEMO_TEST',
        'USER' : 'postgres',
        'HOST': 'localhost',
        'PASSWORD':'12345678abcdef',
        'PORT': '5432',
    

Models.py

from django.db import models
from django.db.models.deletion import SET_NULL
from django.contrib.auth.models import User

class Customer(models.Model):
    user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
    name = models.CharField(max_length=200, null=True)
    phone = models.CharField(max_length=200, null=True)
    email = models.CharField(max_length=200, null=True)
    profile_pic = models.ImageField(default='profile1.png',null=True, blank=True)
    date_created = models.DateTimeField(auto_now_add=True, null=True)
    
    def __str__(self):
        return self.name

class Tag(models.Model):
    name = models.CharField(max_length=200, null=True)
    
    def __str__(self):
        return self.name 

class Product(models.Model):
    CATEGORY = (
            ('Indoor', 'Indoor'),
            ('Out Door', 'Out Door'),
            ) 
    
    name = models.CharField(max_length=200, null=True)
    prince = models.FloatField(null=True)
    category = models.CharField(max_length=200, null=True, choices=CATEGORY)
    description = models.CharField(max_length=200, null=True, blank=True)
    date_created = models.DateTimeField(auto_now_add=True, null=True)
    tags = models.ManyToManyField(Tag)

    def __str__(self):
        return self.name

class Order(models.Model):
    STATUS = (
            ('Pending', 'Pending'),
            ('Out for delivery', 'Out for delivery'),
            ('Delivered', 'Delivered'),
            )
    
    customer = models.ForeignKey(Customer, null=True, on_delete=SET_NULL)
    product = models.ForeignKey(Product, null=True, on_delete=SET_NULL)
    status = models.CharField(max_length=200, null=True, choices=STATUS)
    date_created = models.DateTimeField(auto_now_add=True, null=True)
    note = models.CharField(max_length=1000, null=True)

    def __str__(self):
        return self.product.name
    

我打开django服务器后项目正常运行;问题是当我尝试从终端创建超级用户时。我不确定问题是否来自 Postgresql 数据库与项目之间的连接,或者是否是在 Django 的默认数据库 (SQLITE) 中创建和加载的模型的一些迁移问题。我已经尝试使用makemigrationmigrate 命令再次进行迁移,但是尝试创建超级用户时错误仍然存​​在...一些帮助会很好,非常感谢

【问题讨论】:

表示sqlite中可能存在的组在PostgreSQL数据库中不存在。迁移将创建表等,但不会将 data 从 sqlite 复制/移动到 PostgreSQL 数据库。 我该如何解决?因为我想创建超级用户@WillemVanOnsem 不要将所有内容都以粗体显示。您应该只使用粗体来强调少量文本。 【参考方案1】:

您有一个信号处理程序customer_profile,它试图获取名称为customerGroup。但该组尚未在 PostgreSQL 数据库中创建。

您可以将Group.objects.get(…) 替换为:Group.objects.get_or_create(…)

def customer_profile(sender, instance, created, **kwagrs):
    if created:
        __, group = Group.objects.get_or_create(name='customer')
        instance.groups.add(group)
        Customer.objects.create(
                user = instance,
                name = instance.username
            )
        print('Profile created.!')

【讨论】:

谢谢兄弟@WillemVanOnsem

以上是关于Django 中的项目:我无法在终端中创建超级用户来操作 Postgresql 中的数据的主要内容,如果未能解决你的问题,请参考以下文章

创建超级用户后无法登录 django 管理员帐户

在django中创建帐户后无法登录

在 Elastic Beanstalk Postgresql 中创建 Django 超级用户

django 3.2-在 sqlite3 中创建超级用户时出错

无法在 Django 1.5 中使用自定义用户模型创建超级用户

markdown 如何在文件夹中创建超级终端