Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't create superuser #508

Open
Tracked by #517
vikramiiitm opened this issue Oct 5, 2023 · 4 comments
Open
Tracked by #517

Can't create superuser #508

vikramiiitm opened this issue Oct 5, 2023 · 4 comments

Comments

@vikramiiitm
Copy link

(venv) PS C:\Users\Vikram Chowdhury\Desktop\wagtail_crm_react\Django-CRM> python .\manage.py createsuperuser
Traceback (most recent call last):
File "C:\Users\Vikram Chowdhury\Desktop\wagtail_crm_react\Django-CRM\venv\lib\site-packages\django\db\models\options.py", line 681, in get_field
return self.fields_map[field_name]
KeyError: 'username'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File ".\manage.py", line 24, in
execute_from_command_line(sys.argv)
File "C:\Users\Vikram Chowdhury\Desktop\wagtail_crm_react\Django-CRM\venv\lib\site-packages\django\core\management_init_.py", line 442, in execute_from_command_line
utility.execute()
File "C:\Users\Vikram Chowdhury\Desktop\wagtail_crm_react\Django-CRM\venv\lib\site-packages\django\core\management_init_.py", line 436, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Vikram Chowdhury\Desktop\wagtail_crm_react\Django-CRM\venv\lib\site-packages\django\core\management\base.py", line 404, in run_from_argv
parser = self.create_parser(argv[0], argv[1])
File "C:\Users\Vikram Chowdhury\Desktop\wagtail_crm_react\Django-CRM\venv\lib\site-packages\django\core\management\base.py", line 367, in create_parser
self.add_arguments(parser)
File "C:\Users\Vikram Chowdhury\Desktop\wagtail_crm_react\Django-CRM\venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 61, in add_arguments
field = self.UserModel._meta.get_field(field_name)
File "C:\Users\Vikram Chowdhury\Desktop\wagtail_crm_react\Django-CRM\venv\lib\site-packages\django\db\models\options.py", line 683, in get_field
raise FieldDoesNotExist(
django.core.exceptions.FieldDoesNotExist: User has no field named 'username'

@shaikhmudassir
Copy link
Contributor

I noticed that django.contrib.admin is not listed in INSTALLED_APPS. @ashwin31, don't we need a superuser?

@atlasloewenherz
Copy link

atlasloewenherz commented Nov 6, 2023

this is not a solution, but i was able to create the user following these steps:

  1. create a CustomUserManager to take over the creation of superusers:
class CustomUserManager(BaseUserManager):

    def create_superuser(self, email, password=None, **extra_fields):
        if not email:
            raise ValueError("User must have an email")
        if not password:
            raise ValueError("User must have a password")

        user = self.model(
            email=self.normalize_email(email)
        )
        
        user.set_password(password)
        user.is_superuser = True
        user.staff = True
        user.active = True
        user.save(using=self._db)
        return user
  1. replace the legacy user manager in the User class in common.models.User

from

class User(AbstractBaseUser, PermissionsMixin):
    id = models.UUIDField(
        default=uuid.uuid4, unique=True, editable=False, db_index=True, primary_key=True
    )
    email = models.EmailField(_("email address"), blank=True, unique=True)
    profile_pic = models.CharField(
        max_length=1000, null=True, blank=True
    )
    activation_key = models.CharField(max_length=150, null=True, blank=True)
    key_expires = models.DateTimeField(null=True, blank=True)
    is_active = models.BooleanField(default=True)

    USERNAME_FIELD = "email"
    # REQUIRED_FIELDS = ["username"]


    objects = UserManager()

    class Meta:
        verbose_name = "User"
        verbose_name_plural = "Users"
        db_table = "users"
        ordering = ("-is_active",)

    def __str__(self):
        return self.email

to:

class User(AbstractBaseUser, PermissionsMixin):
    id = models.UUIDField(
        default=uuid.uuid4, unique=True, editable=False, db_index=True, primary_key=True
    )
    email = models.EmailField(_("email address"), blank=True, unique=True)
    profile_pic = models.CharField(
        max_length=1000, null=True, blank=True
    )
    activation_key = models.CharField(max_length=150, null=True, blank=True)
    key_expires = models.DateTimeField(null=True, blank=True)
    is_active = models.BooleanField(default=True)

    USERNAME_FIELD = "email"
    # REQUIRED_FIELDS = ["username"]

    objects = CustomUserManager()

    class Meta:
        verbose_name = "User"
        verbose_name_plural = "Users"
        db_table = "users"
        ordering = ("-is_active",)

    def __str__(self):
        return self.email

this way you can create the superuser.

@ashwin31:
if the above counts as a solution i'll be happy to provide a clean pull request

@RIBINZX RIBINZX mentioned this issue Nov 15, 2023
@amitv9493
Copy link

I tried this. But I am still not able to login to the wagitail admin panel.

@tlaraine
Copy link

thank you, it helped, it’s strange that they didn’t make these changes as a fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants