Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get superuser details in Django?

Tags:

python

django

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 = ...

like image 291
juminet Avatar asked Jul 24 '17 08:07

juminet


People also ask

How do you check if a user is a superuser?

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.

Which command is used to create a superuser in Django?

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.

How do I find my Django admin password?

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.

How can I see my username in Django?

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.


1 Answers

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')
like image 182
Alice Heaton Avatar answered Sep 24 '22 18:09

Alice Heaton