Three methods of Django custom user model _python

Source: Internet
Author: User

Django version:1.7.1

The simplest recommendation:
use Abstractuser to expand fields

Copy 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. Use of Abstractbaseuser

Only when you're on the user
Model inside the default field when you want to use, this method only retains the passwork,last_login,is_active three fields
Refer to official Documentation:
https://docs.djangoproject.com/en/1.7/topics/auth/customizing/

3. Use one-to-one relationships to link related models together

It's much like the way Django was before 1.5. It is ideal for creating third-party expansion packs, loosely coupled, without disrupting the structure of previous projects.

Scenarios that require this method:
-Under your Django PRJ, you want multiple users to have fields that are very different. You may want to combine some of the user's type fields and hope to solve them at the model level.
Examples are as follows:

profiles/models.py

Copy 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 think that in my site, the second most appropriate. 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.

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.