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?
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.
Username: ola Email address: [email protected] Password: Password (again): Superuser created successfully. Return to your browser.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With