给User添加Profile in Django

1. 创建app
manage.py startapp users
2. 编写profile model
from django.db import models
from django.contrib.auth.models import User

GENDER_CHOICES = (
                                    ('M', '男'),
                                    ('F', '女'),
                                    )

class UserProfile(models.Model):
        # 这个字段是必须的,并且只能为user,且要添加外键关联到User
        user = models.ForeignKey(User, unique=True, verbose_name='用户的额外信息')
        # 以下可以按各自需求来定义
        tel = models.CharField('电话', maxlength=20, blank=True, null=True)
        mobile = models.CharField('移动电话', maxlength=20, blank=True, null=True)
        address = models.CharField('家庭地址', maxlength=100, blank=True, null=True)
        website = models.URLField('个人主页', blank=True, null=True)
        birthday = models.DateField('出生日期', blank=True, null=True)
        gender = models.CharField('性别', maxlength=1, choices=GENDER_CHOICES, radio_admin=True, default='M')
        blog = models.URLField('个人主页', blank=True, null=True)
        QQ = models.CharField('QQ', maxlength=50, blank=True, null=True)
        MSN = models.CharField(maxlength=50, blank=True, null=True)
        IM = models.CharField(maxlength=50, blank=True, null=True)
        position = models.CharField('目前所在地', maxlength=200, blank=True, null=True)
        country = models.CharField('目前所在国家', maxlength=50, blank=True, null=True, default='中国')
3. 设置AUTH_PROFILE_MODULE
AUTH_PROFILE_MODULE = 'users.UserProfile' #不区分大小写的
4. 添加INSTALLED_APPS
INSTALLED_APPS = (

...............
        'web.users',
)
5. 同步数据库
manage.py syncdb
只需使用User.get_profile()方法即可返回对应的UserPrfile对象实例了。

原文地址:[url]http://www.cnblogs.com/fengmk2/articles/1147122.html[/url]

你可能感兴趣的:(Web,django,职场,休闲)