I'm new to Django.
I have a simple issue: I need to have the superuser email of the website to send him an email from the admin panel (actually it is for testing if the email is well formated before sending it to registred users).
To get all users, I type:
users = User.objects.all()
How to get the superuser(s)?
superusers = ...
Superuser privileges are given by being UID (userid) 0. grep for the user from the /etc/password file. The first numeric field after the user is the UID and the second is the GID (groupid). If the user is not UID 0, they do not have root privileges.
Django's “Hello World” application Start the terminal by clicking on the “Run” button. Type python3 manage.py createsuperuser in the given terminal and press “Enter”. The system will ask for credentials, after which a superuser will be created. To run the server, we type the command python3 manage.py runserver 0.0.
Retrieve the Python shell using the command "python manage.py shell". Print a list of the users For Python 2 users use the command "print users" For Python 3 users use the command "print(users)" The first user is usually the admin. Select the user you wish to change their password e.g.
You need to call the login method to actually log the user in, or manually handle the login. This post can give you the way to do it. He's also gonna need request context processor enabled.
is_superuser
is a flag on the User model, as you can see in the documentation here:
https://docs.djangoproject.com/en/1.11/ref/contrib/auth/#django.contrib.auth.models.User.is_superuser
So to get the superusers, you would do:
from django.contrib.auth.models import User
superusers = User.objects.filter(is_superuser=True)
And if you directly want to get their emails, you could do:
superusers_emails = User.objects.filter(is_superuser=True).values_list('email')
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