11715

AttributeError: 'UserManager' object has no attribute 'create_superuser'

Question:

I have my customUser manager setup in this way per Django and I am still getting the attribute error. I'm not sure what else can be wrong.

class UserManager(BaseUserManager): def create_user(self, email, date_of_birth, password=None): """ Creates and saves a User with the given email, date of birth and password. """ if not email: raise ValueError('Users must have an email address') user = self.model( email=self.normalize_email(email), ) user.set_password(password) user.save(using=self._db) return user def create_superuser(self, email, date_of_birth, password): """ Creates and saves a superuser with the given email, date of birth and password. """ user = self.create_user(email, password=password, date_of_birth=date_of_birth ) user.is_admin = True user.save(using=self._db) return user class User(AbstractBaseUser): email = models.EmailField( verbose_name='email address', max_length=255, unique=True, ) date_of_birth = models.DateField() is_active = models.BooleanField(default=True) is_admin = models.BooleanField(default=False) objects = UserManager() USERNAME_FIELD = 'email' def get_full_name(self): # The user is identified by their email address return self.email def get_short_name(self): # The user is identified by their email address return self.email def __str__(self): # __unicode__ on Python 2 return self.email

I have defined the user and the superuser. And set the object to UserManager.

Traceback is:

Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line utility.execute() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 393, in run_from_argv self.execute(*args, **cmd_options) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 50, in execute return super(Command, self).execute(*args, **options) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 444, in execute output = self.handle(*args, **options) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 149, in handle self.UserModel._default_manager.db_manager(database).create_superuser(**user_data) AttributeError: 'UserManager' object has no attribute 'create_superuser'

The code I am running is:

python manage.py createsuperuser

Answer1:

I supposed that you have Django 1.8. Here is what you have to do to create your own models which inherit of AbstractUser model.

In your models :

<pre class="lang-python prettyprint-override">from django.contrib.auth.models import AbstractUser from django.db import models class User(AbstractUser): pass

<strong>Your User model will automatically inherit of the UserManager of AbstractUser</strong> then you don't have to write : objects = YourUserManager() unless you want to extend it. In this case, you can do this :

<pre class="lang-python prettyprint-override">from django.contrib.auth.models import AbstractUser, UserManager as AbstractUserManager from django.db import models class UserManager(AbstractUserManager): pass class User(AbstractUser): objects = UserManager()

Define the User model in settings.py used in your Django apps :

# <module_name>.<user_model_name> AUTH_USER_MODEL = 'user.User'

Now, when you want to import your user model in an other app to use it, use settings.AUTH_USER_MODEL. here is an example :

<pre class="lang-python prettyprint-override">from django.db import models from django.conf import settings class Token(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL)

Read the right Django documentation for more information : <a href="https://docs.djangoproject.com/fr/1.8/topics/auth/customizing/#extending-the-existing-user-model" rel="nofollow">Extending the existing user model</a>

Answer2:

Try this and replace the fields according to your requirements:

class UserManager(BaseUserManager): def create_user(self, email, password=None,is_active=True,is_staff=False,is_admin=False): """ Creates and saves a User with the given email and password. """ if not email: raise ValueError('Users must have an email address') user = self.model( email=self.normalize_email(email), ) user.set_password(password) user.save(using=self._db) return user def create_staffuser(self, email, password): """ Creates and saves a staff user with the given email and password. """ user = self.create_user( email, password=password, ) user.staff = True user.save(using=self._db) return user def create_superuser(self, email, password): """ Creates and saves a superuser with the given email and password. """ user = self.create_user( email, password=password, ) user.staff = True user.admin = True user.save(using=self._db) return user

Recommend

  • Wrong offset for TimeZone casablanca (java)
  • ISNULL returns 0 for a hardcoded column with NULL value
  • Changing a specific char in a MySQL string
  • Is a C# struct ever boxed when used as the return value of a function?
  • RecyclerView lagging inside ScrollView
  • How to update the learning rate in a two layered multi-layered perceptron?
  • Unity3D display Google Admob slow
  • Jquery Tabs - Flash of Unstyled Contnet (FOUC)
  • Union two different tables with a primary ID field
  • How to set current cell on SAP GUIContainerShell in C#?
  • making list clickable to load ajax request into div
  • Codename One - container.setScrollVisible(true) in the center of a BorderLayout
  • how to refresh alertdialog in flutter
  • Keycloak create a custom identity provider mapper
  • Azure function C#: Create or replace document in cosmos db on HTTP request
  • Flask-Admin batch action with form
  • Executing git-hooks on windows errors out
  • Add a new custom default ordering catalog option in Woocommerce
  • Find nearest known location: Google Reverse Geocoding
  • How to change the default browser in visual studio code latest released?
  • Sending a PDF created dynamically as an attachment using PDFKit in a nodejs application
  • Reading large text file very slow
  • Selenium - how to switch to a different (login) window that is brought up?
  • Existing data serialized as hash produces error when upgrading to Rails 5
  • JSF validateLength question
  • Coin change recursive approach
  • Inet6Address valid for invalid IPv6 Address
  • Compiling and linking NASM and 64-bit C code together into a bootloader [duplicate]
  • how do i compare two rows and store the similarities of the two rows in another column
  • Android: Unable to detect vertical plane
  • Debug `Unexpected end of JSON input Error` on content script
  • Apple Mach-O Linker error (“duplicate symbol”)
  • Angular FormGroup won't update it's value immediately after patchValue or setValue
  • Create/delete users from text file using Bash script