When I’m working with Jiango, one of the first things I need to do often is to work with users-such as getting a login user, making new, or increasing the default user model to add more information.
Now, the built -in in Jiango is User
Model, but sometimes you want a customs. If you are just starting, things can be a bit confused.
Good news? It is very easy to get a user model in Jiango when you understand how Jiango is arranged.
Today, explain why I go through how to get the user model in Jingo, why you show the real code you can use, and answers some common questions that are usually around the subject.
Let’s jump in it.
The table of content
Why is it important to get the user’s model correctly
Before anything, it is important to know that it also makes a difference.
Jiango Projects relies heavily on user information – not only for login, but also for permissions, profiles, admin management and more.
If you get the user’s model incorrectly, you can easily suffer from later problems, especially if you customize your user model.
Jiango also warns you about it Official documents. If you do not use the right method to access the user model, your project may be broken when you change or increase it.
This is why the user model is always very important to achieve Recommended The way, which I will show you next.
How to get a user model in Jingo
Well, the easiest way to get a user model in Jingo is that:
from django.contrib.auth import get_user_model
User = get_user_model()
What’s going on here?
get_user_model()
A built -in is the Jiango function.It returns the right user model – whether you are using a default or custom.
If you are wondering why not only imported from django.contrib.auth.models import User
The reason for this, this is: If you ever change the default user model for a customs, thus direct importing your code breaks down.
Using get_user_model()
You give your plan a safe and future proof.
Full example: User model use
Let’s see not just a small piece but a full working example.
Imagine that you would like to create a new user inside Jiango View:
from django.contrib.auth import get_user_model
from django.http import HttpResponse
def create_user_view(request):
User = get_user_model()
user = User.objects.create_user(username='newuser', password='securepassword123')
return HttpResponse(f"Created user: {user.username}")
In this instance:
First, I get with the user model
get_user_model()
.After that, I use Jiango’s built -in
create_user
How to make the user safely.Finally, I send back a simple HTTP response by displaying the username created.
Consider how neat and flexibility it is – no matter which user you are using under the model hood.
When using settings.AUTH_USER_MODEL
Another thing you will often see in Jiango Projects is something like:
from django.conf import settings
settings.AUTH_USER_MODEL
It does not Get User model. Instead, it gives you Wire The way the user model, such as "auth.User"
(For the default) or "myapp.MyCustomUser"
If you have made it customized.
You usually use settings.AUTH_USER_MODEL
Inside you Models.p When you want to link to the user’s model in a foreign, Venetoon field, or Montomisio field.
For example:
from django.conf import settings
from django.db import models
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
bio = models.TextField()
Here, Profile
The model is connected to the right user model. Once again, this gives your project flexible and the future evidence.
Quick Summary
Situation | What to use |
Obtaining the original user model in the Azgar code (visit, form, admin, and so on) | get_user_model() |
Referring to the user model in a database relationship (foreigner, Winetone Field, and so on) | settings.AUTH_USER_MODEL |
Remember this table – it saves a lot of headache later!
To avoid ordinary errors
Directly imported
User
: Never dofrom django.contrib.auth.models import User
Unless you are 100 % sure you are sticking to the default model forever (not suggesting).Hard coding relationship: If you write something
ForeignKey('auth.User')
To use instead ofsettings.AUTH_USER_MODEL
If you ever switch to the customer model, it will be broken.Do not make the soon -custom customer model: If you think you may ever need a custom user model (such as phone number, add additional profile fields), set it up soon. Once you have a database full of users, it is painful to switch later.
Normal questionnaire
1. Can I have access? request.user
Straight?
Yes! The inside view, request.user
Provides you with an existing login user object. Behind the curtain, Jiango is using the right user model, whether customs or default.
2. What happens if I call get_user_model()
Many times?
There is no problem. Jiango catchs it internally, so it is effective. Call it wherever you need it.
3. How can I know if I am using the customer model?
Check your Jiango settings file (settings.py
) And find AUTH_USER_MODEL
. If this set is (liked 'myapp.MyCustomUser'
You are using the customs model. If it is not there, Jiango is using default.
4. When should I create a customer model?
If you too Think You will need phone number, date of birth, profile pictures, as well as fields, it is better to set up a custom model soon.
Here is an excellent leader by Jiango’s official documents Customize the user model.
Conclusion
There is no need to work with consumers in Jiango. Once you know how to use get_user_model()
When you need a model and settings.AUTH_USER_MODEL
The database relationship, your code is clean, safe and ready, whatever changes you have in your way.
Now that you know how to get a user model in Jiango, what would you like to customize your users in your project? Shoot me a message X.
If you want to show you how to show you Set From the beginning a custom user model, let me know – once you know the steps, it’s not difficult.