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对象实例了。
---------------------
原文:https://blog.csdn.net/Java2King/article/details/5333724
原文地址:https://www.cnblogs.com/Treasuremy/p/9885697.html
时间: 2024-10-11 07:20:55