How Django Notes extend the fields of the user table

Source: Internet
Author: User
Tags auth class definition extend http request create database

Django comes with a permission framework where the fields of the Auth_User table are difficult to meet the normal requirements and therefore need to be extended, as for extensions, there are generally several options:
1. Directly modify the Django source code, modify the user class definition, and various methods, and then expand the Database Auth_User table field to match their own needs. (Source code: Django.contrib.auth.models import User), this way, every time you upgrade Django is very careful.
2. Copy the source code of the Django user and the authentication part to your app, then modify and configure it so that you don't need to change the Django code. But if you're going to upgrade Django, you might be in trouble.
3. Inherit the user and do the extension.
4. Django's official recommended method, Profile mode extension. Method One

Compare these several ways, found that 1, 2 methods are very yellow and violent. So not in very special cases, it is not recommended. The 3rd method, I currently test down is can, but after the extension, enter the password when it is actually clear text, and saved into the database is not encrypted password, is still looking for reasons, so here the 4th method is introduced.

In, add the following class to extend the user:

#================== Extended User ====================================
class UserProfile (models. Model):
    user = models. Onetoonefield (User)    
    major = models. TextField (default= ", blank=true)
    address = models. Charfield (max_length=200,default= ", blank=true)

    def __unicode__ (self):
        return self.user.username

def Create_user_profile (sender, instance, created, **kwargs): "" "
    Create the userprofile when a new user is saved" "" "
    I F Created: Profile
        = UserProfile ()
        Profile.user = Instance ()

#post_save. Connect ( Create_user_profile, Sender=user)
"" "Do not understand that I must comment out the above line, will not be wrong, otherwise there will be duplicate entry ' 2 ' for key ' user_id '"), to see that means that Repeated, but do not understand why repeat, comment off the above, everything is normal, but with the official documents again, confusing "" "
#================== Extended User End ================================

Also need to modify

"" "User Module Extension" ""
class Profileinline (admin. Stackedinline):
    model = userprofile
    #fk_name = ' user '
    max_num = 1
    can_delete = False

class Customuseradmin (useradmin):
    inlines = [Profileinline,] (User) (user, Customuseradmin) "" "" "" "" "" "" "

Modify the configuration of the to increase

"" "User Module Extension section" ""
auth_profile_module = ' djangoadmin.myadmin.UserProfile ' "" "
User Module extension Complete" "

According to the official explanation, here is the name of the app label plus the extension class. The name of the app that should be created is the official recommended way to use "." In two parts. Connected, I have three parts here, there is no error.
Then run the Python syncdb, which is the table that will create the response in the database, and has user_id this foreign key field.
Finally, let's run the program and go into the Add user interface, and you'll see that the fields you've expanded are displayed.
Each time the user is added, the corresponding data is added to the expanded table, and when modified, the corresponding data is modified, which is correlated by user_id, thus completing the extension of the user model.
If you want to get the contents of an extension table, you can get it by Request.user.get_profile (). Address. After the user object is obtained, it is convenient to get the extended class. Method Two

If you want to inherit the user's way to do, you can use the following methods, reproduced from the Internet:
1. Create a user profile class that inherits directly from the user
Program Code Program code

From django.contrib.auth.models import User, Usermanager
class Customuser (user): 
    timezone = models. Charfield (max_length=50, default= ' Asia/shanghai ')   
    objects = Usermanager ()

In this way, the user = CustomUser.objects.create (...) can be used directly. method to create a new object. And if you use Request.user to get the object that you extend, you can take the property directly. No need for get_profile.

2. Extended authentication mechanism
Program Code Program code

From django.conf import settings from django.contrib.auth.backends import modelbackend from django.core.exceptions Import improperlyconfigured from django.db.models Import Get_model class Customusermodelbackend (modelbackend): def au Thenticate (self, Username=none, password=none): Try:user = Self.user_class.objects.get (username=usern AME) if User.check_password (password): return user except Self.user_class. Doesnotexist:return None def get_user (self, user_id): Try:return self.user_class.ob Jects.get (pk=user_id) except Self.user_class. Doesnotexist:return None @property def user_class (self): If isn't hasattr (self, ' _user_class ') : Self._user_class = Get_model (*settings. Custom_user_model.split ('. ', 2)) if not self._user_class:raise improperlyconfigured (' Could No t get custom user model ') return Self._user_class
Program Code Program code
Authentication_backends = (
   ' myproject.auth_backends. Customusermodelbackend ',
Run Python syncdb
A database is also generated, but the fields of the table are not used with the previous method.

Then run, the same test, can achieve the same effect, but the interface on the password input where I am always dissatisfied, so I did not use. Method Three

Django User model:
The difference between Django and many other web frameworks is that it comes with a ready-made user model, because 99% of web development involves the concept of a user, Not only does it solve the need to repeat the work of creating a new user table each time a project is developed. And the biggest benefit is the Django model-specific processing, only through the Django.contrib.auth.models import user model, then regardless of which app you come from, each app is referenced by the same User model, The actual foreign key generated in the database after those fields are pointed to the user model via Foreignkey,manytomanyfield,onetoonefield is the ID of the reference auth_user table. This is an indispensable mechanism for many apps that are independent but need a user model, because if Django does not have the built-in user models, then each app has to be isolated, defining the user model independently, and the end result is that when it comes to combining these apps, Because the user model is not unified, it takes a lot of time to modify the user model-related code for these apps. The most obvious example of this is the Pinax project (, a SNS project that combines a large number of open source apps and is simply configured to assemble them.

Problems with the Django User model:
Although Django comes with a user model that facilitates a lot of apps, it's important that every app needs to add some additional information to the user model, and that the field defined in the Django-based user model is a regular message. Must not meet the needs of the app itself. To do this, you need to add a profile to put the user's additional information. This is mainly by the following ways:
1. The way Django is officially directed:
See document
The approximate process is this: Define a profile model, which contains a unique=true ForeignKey, in add an option Auth_profile_module point to the profile model, Then the example of the user model can be obtained by get_profile () to obtain the corresponding profile information, although this method seems very convenient, but one of the obvious disadvantage is that it is in an additional table, although the Get_profile () Cached the results of the first fetch of the profile, but each HTTP request to do a query for this, when the number of users, the impact on the performance is relatively large. Another problem is what to do when you need to define a manytomanyfield such as friends that points to ' self '. Definition in the profile is very unreasonable, a user and another user to make friends, not with the profile of Ta do friends, not to mention the definition in the profile, as the friends of the reverse query, obtained queryset is profiles, When you want to get these profile-directed user information, you have to do a lot of queries.

2. Modify Django's source code directly on the user model to add fields:
This method is very straightforward and obvious, if it is used as a final release version of 1.0.2-final, and it is not intended to update the Django version later, it is also possible. But if you use SVN development version, the way to modify the source is a bit violent.

3, directly discard the Django user model, you define a user model, including their own management auth, write auth backends:
I have seen a few projects are done, although by inheriting the original user Model,modelbackend class, you can only write very little code can be implemented, this way in fact, modify Django source code is no big difference, is to reduce a profile table, Only the extended user model is placed in the Django source code, one is placed in its own source, and this way than to modify the source is worse, this destroys the first mentioned in combination with other apps of the possibility.

Do not modify the Django source code or add an extended user model for the profile table:
The various methods mentioned above are mainly about modifying the Django source code and adding a profile table between the pros and cons of the expansion. So is there a way of both worlds, neither modifying the source code nor adding the profile table. The answer is yes, and in the early Django version, this is even a very common way, but later Django changed the model part of the source, blocking this way, because it seems too magic.
The inner meta in the previous Django model can define a replaces_module parameter that specifies that you want to replace the model with another model, which looks strange and magical, but it's confusing to manage. So the last feature was canceled.
Although the above way is blocked, but still can be changed in other ways the user model class, said so much, or directly look at the source to more clear it:

  From django.db import models from django.contrib.auth.models import User from django.contrib.auth.admin import UserA DMin Import datetime class ProfileBase (type): Def __new__ (CLS, name, bases, attrs): module = Attrs.pop (' __module__ ') parents = [B for B in bases if Isinstance (b, ProfileBase)] if Parents:fi ELDs = [] for obj_name, obj in Attrs.items (): If Isinstance (obj, models. Field): Fields.Append (obj_name) user.add_to_class (obj_name, obj) useradmin.fieldsets = Lis T (useradmin.fieldsets) UserAdmin.fieldsets.append ((name, {' Fields ': Fields}) "Return Super (profile Base, CLS). __new__ (CLS, name, bases, Attrs) class profile (object): __metaclass__ = ProfileBase class Myprofile (profile): Nickname = models. Charfield (max_length = 255) Birthday = models. Datetimefield (Null = true, blank = true) City = models. Charfield (Max_leNgth = $, blank = True) University = models. Charfield (max_length = 255, blank = True) def is_today_birthday (self): return () = = Dat ()

The above code defines a profilebase meta-class, and then defines a profile class as the profile base class, and its meta class is profilebase.
The function of a meta-class is simply to create classes of other classes. The example of a meta-class is a normal class, and an instance of an ordinary class is an ordinary instance.

It doesn't matter if you don't understand this, just know that in the code above, when the interpreter sees you defining a subclass of a profile class, and the meta class of the profile class is profilebase, so Myprofilede's meta class is also profilebase, That is, when you define a subclass of any profile, it executes the code in new in the meta-class profilebase, and passes the class (name, base class, Class attribute) that is being defined as a parameter to new.

The field in Myprofilede uses the same declaration syntax as the normal model, so the field and the custom method Is_today_birthday passed in as a property in Attrs, and the meta-class extracts the information through
User.add_to_class (Obj_name, obj) is added to the user class, and Add_to_class is also the way in which field is defined in the normal model, because the common model also has a meta class that is doing something similar, Only the Meta class defined here specifically adds something to the user model.

After adding these fields and custom methods, the previously recognized attributes are field and then added to Useradmin.fieldsets, so that the new field can be edited with the other user fields in Admin.

If you have other apps that you want to add field or method to the user model, just pass the subclass profile and define it using declarative syntax, and all other work has a meta-class to help you do it.

The effect of the field and method added to the user model in this way is the same as modifying the Django User model's source code, but now these changes do not have to be as violent as modifying the source code, and you can add it whenever you want.

Places to be aware of:
1, the definition of the above meta-class includes the definition of myprofile, preferably, like the normal model, placed in the app's file, so that Django can easily encounter these definitions, or Django did not see the code will not be effective, and placing it in other places is very prone to import dependency issues.

2, if you have synchronized the database through SYNCDB, and then use this method to expand the user model, then Django know that you defined these fields, but again run syncdb, no longer for these field automatically create database field, Because Django's syncdb is only responsible for creating fields for new tables. This is the same case that you have defined a model and run SYNCDB, and then add fields to that model. Reference:


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: 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.