Traditional Culture Encyclopedia - Photography major - How to use the user model correctly

How to use the user model correctly

1. Confirm the user model

We recommend a method to determine the user model used in the django project:

# When using the default user model

& gt& gt& gt import get_user_model from django.contrib.auth

& gt& gt& gtget_user_model()

& ltclass ' django . contrib . auth . models . user ' & gt;

# When using a custom user model

& gt& gt& gt import get_user_model from django.contrib.auth

& gt& gt& gtget_user_model()

& ltclass ' XXX . models . user profile ' & gt;

2. Use the settings. Authenticated user model.

Starting from django 1.5, users can customize the user model. If you need to use the user model for foreign keys, the officially recommended method is as follows:

Set AUTH_USER_MODEL in the settings:

# settings.py

# format is ". & lt model name > "

AUTH_USER_MODEL = "myapp。 New user "

For models.py

# models.py

Import settings from django.conf

Import model from django.db

Class articles (models. Model):

Author = model. ForeignKey (setting. Authorization _ User _ Model)

Title = Model. CharField(max_length=255)

Also, don't use get_user_model () in foreign keys.

3. Customize the user model

Method 1: extends the AbstractUser class.

If you are satisfied with the user model that comes with django and want an extra field, you can extend the AbstractUser class:

# myapp/models.py

Import AbstractUser from django.contrib.auth.models

Import model from django.db

Class NewUser(AbstractUser):

New_field = model. CharField(max_length= 100)

Don't forget the settings:

AUTH_USER_MODEL = "myapp。 New user "

Method 2: extend the AbstractBaseUser class

AbstractBaseUser contains only three fields: password, last _ login and is_active. If you are not satisfied with the default first _ name and last _ name of django user model, or just want to keep the default password storage method, you can choose this method.

Method 3: Use OneToOneField

If you want to build a third-party module to publish on PyPi, this module needs to store additional information of each user according to the user. Or in our django project, we want different users to have different fields, and some users need different field combinations. We use method 1 or method 2:

# profiles/models.py

Import settings from django.conf

Import model from django.db

From the flavor. Model import flavor

Category EasterProfile (model. Model):

User = model. OneToOneField (setting. Authorization _ User _ Model)

Favorite _ ice cream = model. ForeignKey(Flavor,null=True,blank=True)

Category ScooperProfile (model. Model):

User = model. OneToOneField (setting. Authorization _ User _ Model)

Scoops_scooped = model. Between fields (default =0)

Category list configuration file (model. Model):

User = model. OneToOneField (setting. Authorization _ User _ Model)

Flavor _ invention = model. ManyToManyField(Flavor,null=True,blank=True)

Using the above method, we can use user.easterprofile.favorite _ ice _ cream to get the corresponding configuration file.

The disadvantage of using this method may be that it increases the complexity of the code.