Three ways to customize the user model for Django

Source: Internet
Author: User
Django version:1.7.1

The simplest of recommendations:
Expand Fields with Abstractuser

Copy the Code code as follows:


profiles/models.py

From django.db import Models
From Django.contrib.auth.models import Abstractuser
From django.utils.translation import Ugettext_lazy as _

# Create your models here.
Class Karmauser (Abstractuser):
Karma = models. Positiveintegerfield (_ ("Karma"), Default=0,blank=true)
settings.py

Auth_user_model = ' profiles. Karmauser '

2. Using Abstractbaseuser

Only when you are on the user
The default field in the model is not used until it is satisfied, this method only retains the passwork,last_login,is_active three fields
Refer to the official documentation:
https://docs.djangoproject.com/en/1.7/topics/auth/customizing/

3. Use a one-to-one relationship to link related models together

This is much like the way Django 1.5 was. Ideal for creating third-party expansion package scenarios, loosely coupled without destroying the structure of the previous project.

Scenarios where this method is required:
-In your own Django PRJ, you want multiple users to have very different fields. You might want some users to combine some type of user field and want to be able to solve these problems at the model level.
Examples are as follows:

profiles/models.py

Copy the Code code as follows:


From django.conf Import settings
From django.db import Models

From Flavors.models import Flavor

Class Eaterprofile (models. Model):
# Default User profile
user = models. Onetoonefield (settings. Auth_user_model)
Favorite_ice_cream = models. ForeignKey (Flavor,null=true,blank=true)

Class Scooperprofile (models. Model):
user = models. Onetoonefield (settings. Auth_user_model)
scoops_scooped = models. Integerfield (default=0)

Class Inventorprofile (models. Model):
user = models. Onetoonefield (settings. Auth_user_model)
flavors_invented = models. Manytomanyfield (Flavor,null=true,blank=true)

Personally, I think the second is the most appropriate in my site. Testing whether the model can be simplified with abstract classes. To be Continued ...

The above 3 kinds of methods have advantages and disadvantages, everyone according to their own needs, free choice bar.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.