Home > Software design >  I can't create super user postgresql and Django
I can't create super user postgresql and Django

Time:12-19

I am doing a project with Django in which I try to change the database from SQLITE to Postqresql. When I try to create the super user I get this error.

Traceback (most recent call last):
  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
    

The project works normally when I turn on the Django server; the problem is when I try to create the superuser from the terminal. I am not sure if the problem comes from the connection between the Postgresql database with the project, or if it is some problem of migration of the models created and loaded in Django's default database (SQLITE) ... It would be excellent some help, thank you very much

CodePudding user response:

Make sure you gave installed psycopg2 which is adapter to postresql for python. Also make sure you've applied all migrations to your db.

P.S It sounds like somewhere in your code you've wrote: group = Group.objects.get(name='customer'). This group object does not exist and couse of thath django throw an Exception.

CodePudding user response:

Try to make the migrations one more time:python manage.py makemigrations && python manage.py migrate and then python manage.py createsuperuser.

  • Related