Python Django Get User Model Example

Published On: 08/09/2022 | Category: Django


Hi Dev,

Now, let's see tutorial of python django get_user_model. This article will give you simple example of django get user model. I explained simply step by step django user model example. you can understand a concept of django get_user_model(). Follow bellow tutorial step of how to get django user model.

Django, in setting related to the User model with the name AUTH_USER_MODEL. This setting represents the model to be used for the User.

So, if this AUTH_USER_MODEL setting is changed to some other user model then we cannot use the User model class directly.

let's see bellow example here you will learn django get_user_model example.

Django Admin Interface:



Example: 1

In this example how to use User model class, we can use the get_user_model() method. The get_user_model() method returns the currently active user model. Let’s take an example and understand how to use this get_user_model() method. The code for the example is as follows.

views.py
from django.contrib.auth import get_user_model
from django.http import HttpResponse

def index(request):
    user = get_user_model()
    users = user.objects.all()

    return HttpResponse(users)
Output
<QuerySet [<User: admin>, <User: bhavesh>]>

I Hope It will help you....

Happy Coding!