Problem with createuperuser when implementing user user model

I am trying to implement my own custom model in Django 1.6, but I am getting this error.

Traceback (most recent call last): File "./manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/Users/gabriel/.virtualenvs/hang_server/lib/python3.4/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line utility.execute() File "/Users/gabriel/.virtualenvs/hang_server/lib/python3.4/site-packages/django/core/management/__init__.py", line 392, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/Users/gabriel/.virtualenvs/hang_server/lib/python3.4/site-packages/django/core/management/base.py", line 242, in run_from_argv self.execute(*args, **options.__dict__) File "/Users/gabriel/.virtualenvs/hang_server/lib/python3.4/site-packages/django/core/management/base.py", line 285, in execute output = self.handle(*args, **options) File "/Users/gabriel/.virtualenvs/hang_server/lib/python3.4/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 141, in handle self.UserModel._default_manager.db_manager(database).create_superuser(**user_data) TypeError: create_superuser() missing 1 required positional argument: 'email' 

Here is my UserManager

 class UserManager(BaseUserManager): def _create_user(self, username, email, password, is_staff, is_superuser, **extra_fields): now = timezone.now() email = self.normalize_email(email) user = self.model(username=username, email=email, is_staff=is_staff, is_active=False, is_superuser=is_superuser, last_login=now, date_joined=now, **extra_fields) user.set_password(password) user.save(using=self._db) return user def create_user(self, username, email=None, password=None, **extra_fields): return self._create_user(username, email, password, False, False, **extra_fields) def create_superuser(self, username, email, password, **extra_fields): user=self._create_user(username, email, password, True, True, **extra_fields) user.is_active=True user.save(using=self._db) return user 

It seems like it would be pretty straight forward, but I can't understand why I get this error in the first place, because I have the email address specified in my create_superuser function. I have looked through several online tutorials and don’t see how it is implemented in different ways. Can someone explain what I'm doing wrong?

+6
source share
2 answers

Looking at the code for the control commands, it only requests fields in the REQUIRED_FIELDS user model (as well as the username). This attribute contains the default in AbstractBaseUser, but if you redefined it or did not inherit from this model (what you should do) - then the letter will not be requested, but not passed to the create_superuser method.

+10
source
 class User(AbstractBaseUser, PermissionsMixin): username = models.CharField('username', max_length=150, unique=True) email = models.EmailField('email address', unique=True, max_length = 255) USERNAME_FIELD = 'username' REQUIRED_FIELDS = ['email'] 

For me, the above code just solved the problems.

0
source

Source: https://habr.com/ru/post/973638/


All Articles