22

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

1 Answer 1

50

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')
3
  • This is good, but how would you get the currently logged in superuser's details?
    – pymat
    Commented Nov 16, 2017 at 15:17
  • 5
    Before you can use User model, you have to do from django.contrib.auth.models import User
    – etlds
    Commented Nov 27, 2018 at 20:18
  • if you got AttributeError: Manager isn't available; 'auth.User' has been swapped for 'account.User', use ` from django.contrib.auth import get_user_model User = get_user_model() User.objects.filter(is_superuser=True) `
    – pnkjmndhl
    Commented Apr 6, 2023 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.