django Extended User
surroundings
Python 3.5.1
django 1.9.1
Foreword
Today, with django write web platform, the first thought that comes django authentication session provides even better, since there are the wheels, and we do not need their own built.
Extension of method django user:
A rewrite user, will register a new user to the admin, but also to rewrite certification
Second, inherited user, expand (remember to set the settings in AUTH_USER_MODEL
)
2.1 class inherits AbstractUser
If you are satisfied with django comes with the User model, and hope to add additional field, you can extend AbstractUser class (this paper is to implement this method)
The new django User class supports email, you can also use email as a user login
2.2 class inherits AbstractBaseUser
AbstractBaseUser contains only three field: password, last_login and is_active this is your own highly customized things they need.
model.py
admin.py
Three, profile way to expand, but from the beginning to give up such an approach django1.6
Fourth, the online method to find, do not change the source code, without the new table, extended user
Explain a little this code: ProfileBase is a custom metaclass inherits from types.ClassType
which ProfileUser a base class, its metaclass is ProfileBase, but what we really ExtraInfo custom field type, reason why the base class ProfileUser and ExtraInfo separately, for ease of reference ProfileUser elsewhere, for custom extensions. Simply put, when you see the interpreter in the definition of a subclass ProfileUser class and metaclass ProfileUser class is ProfileBase, so ExtraInfo metaclass is ProfileBase, in the subclass definition ProfileUser when it performs yuan class ProfileBase the new code, and is defined in class (name of the base class, class attributes) passed as a parameter to the new, name here is the name of the class ExtraInfo, attrs medium contains your newly added field, by User.add_to_class
the the new field is added to the User, in order to be able to show up in the admin, add it to the UserAdmin.fieldsets
middle, so you can edit this field in the background, of course, you can also add to ist_display, so that it appears in the list.
If you have any other app would also like to add to the User Model in the field or method, as long as all subclasses ProfileUser by category, and then use declarative syntax to define all other work has metaclass help you to complete. This is also the inner workings of all django model, you can use this method to extend any model.
Reprinted Source: http://www.opscoder.info/extend_user.html
demand
Register Login have ready-made code that comes with the User field primarily only (email, username, password), it is necessary to expand User, we need to increase the field
code show as below:
model.py
settings.py
Stepped pit:
1, after the extended user table, to add in settings.py
2, authentication settings in the background to add, particularly remember a comma, otherwise an error
Certified background without being given
The error did not add a comma
form.py
views.py