django自带User管理中添加自己的字段方法

时间:2024-10-08 13:33:05
#coding=utf-8
from django.db import models
from django.contrib.auth.models import User, make_password, check_password, Group, GroupManager
from django.contrib.auth.admin import UserAdmin
from OnLineAnswer.settings import UPLOADTO # Create your models here.
class WebPowers(models.Model):
powername = models.CharField(max_length=250)
def __unicode__(self):
return self.powername class ProfileBase(type): #对于传统类,他们的元类都是types.ClassType
def __new__(cls, name, bases, attrs): #带参数的构造器,__new__一般用于设置不变数据类型的子类
module = attrs.pop('__module__')
parents = [b for b in bases if isinstance(b, ProfileBase)]
if parents:
fields = []
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 = list(UserAdmin.fieldsets)
UserAdmin.fieldsets.append((name, {'fields': fields}))
return super(ProfileBase, cls).__new__(cls, name, bases, attrs) class ProfileUser(object):
__metaclass__ = ProfileBase #类属性 class MyProfile(ProfileUser):
headImage = models.ImageField(upload_to=UPLOADTO, null=True, blank=True, verbose_name="头像")