juminet
juminet

Reputation: 497

How to get superuser details in 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 = ...

Upvotes: 22

Views: 54546

Answers (1)

Alice Heaton
Alice Heaton

Reputation: 1330

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')

Upvotes: 51

Related Questions