django 자체 User 관리에 자신의 필드 추가 방법

1352 단어 django
#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="  ")


좋은 웹페이지 즐겨찾기