Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use an existing user as Django admin when enabling admin for the first time?

I have built a Django site for a while, but I never enabled Django admin.

User accounts are registered on both LDAP and Django, but the master record is based on LDAP. So I must use the account in LDAP as super user.

When I enable Django Admin, I am prompted to create a super user. Can I use an existing account (registered on both LDAP and Django db) as super user?

How?

like image 684
User007 Avatar asked Jul 05 '12 03:07

User007


People also ask

Can I use Django admin in production?

Django's Admin is amazing. A built-in and fully functional interface that quickly gets in and allows data entry is priceless. Developers can focus on building additional functionality instead of creating dummy interfaces to interact with the database.

What is the default username and password for Django admin?

Username: ola Email address: [email protected] Password: Password (again): Superuser created successfully. Return to your browser.


2 Answers

Yes, but you'll do it through the Django shell:

python manage.py shell 

Then fetch your user from the database:

from django.contrib.auth.models import User user = User.objects.get(username="myname") user.is_staff = True user.is_admin = True user.save() 

Exit the shell, and that user will now be an admin user.

You can also add the line

user.is_superuser = True 

before calling user.save() if you want or need this user to be a superuser and have all the available permissions.

like image 167
Herman Schaaf Avatar answered Oct 14 '22 00:10

Herman Schaaf


The accepted answer generated an error on Django 3:

AttributeError: Manager isn't available; 'auth.User' has been swapped for 'users.User' 

From Django shell:

python manage.py shell 

Run this:

from django.contrib.auth import get_user_model User = get_user_model() user = User.objects.get(username="myname") user.is_staff = True user.is_admin = True user.is_superuser = True user.save() 
like image 29
Carlos Oliveira Avatar answered Oct 14 '22 00:10

Carlos Oliveira