Search code examples
djangodjango-modelsdjango-users

How to set is_staff to True in Django when a user is created with a specific group selected


How would I automatically set is_staff to True when creating a new user in the Django admin when they have a specific group selected?


Solution

  • The best way to achieve it using the django signal.

    Just listen when a user created or updated its group. when it is created or updated with the specific group selected, You just set is_staff=True and save it.

    To learn more about Django signal: https://docs.djangoproject.com/en/3.2/topics/signals/

    Learn more about Django group and permission https://docs.djangoproject.com/en/3.2/ref/contrib/auth/#django.contrib.auth.models.User.groups