Search code examples
python-3.xdjangoadmin

How to create two users of different permissions in Django?


How to set up two different types of users in Django: superusers, who have access to all system features, and maintenance users, who can perform system maintenance. These are users created in the backend management system, not logged-in users in the front-end interface. They are created automatically when the system is initialized. Thanks in advance.

As a beginner, there aren't too many ideas. I'm just going to create an initial user. I'd love for ideas and related reference codes. Thank you here. The documentation is very detailed, but I just Django framework, I don't understand, sorry, can you provide a specific approach?


Solution

  • Django's builtin User model [Django-doc] has already two fields .is_staff [Django-doc] and .is_superuser [Django-doc] are booleans to "promote" a user to a staff account and a superuser account.

    If you have a view that should only be accessible to staff users, you can use the @staff_member_required [Django-doc] decorator:

    from django.contrib.admin.views.decorators import staff_member_required
    
    
    @staff_member_required
    def my_view(request):
        # …
        pass

    then the my_view will bounce if the user is not a User with is_staff is True.