Django 中如何给 User 增加额外的 Profile 信息
来源:互联网 发布:汽车修理软件哪个好 编辑:程序博客网 时间:2024/05/02 06:10
摘要:http://www.b-list.org/weblog/2006/06/06/django-tips-extending-user-model
这里的做法是定义一个独立的模型及独立的数据表来表示 profile.
1. 首先添加一个 model 来专门定义 profile:
(注意上面代码里用 ForeignKey 搭配 unique=True 的设置,取代了老的做法 OneToOneField)
2. 然后在 settings.py 里面设置一下,让 Django 的框架知道你定义了这个模型作为 profile:
这一步需要特别当心,只需要写 appname.ModelName 即可。如果写多了,则可能出现 "too many values to unpack" 的错误。
3. 当然还需要执行一下同步数据库操作:
4. 现在可以利用内建的 User 模型的 get_profile 方法获取到对应的 Profile 对象:
这里的做法是定义一个独立的模型及独立的数据表来表示 profile.
1. 首先添加一个 model 来专门定义 profile:
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
url = models.URLField()
home_address = models.TextField()
phone_numer = models.PhoneNumberField()
user = models.ForeignKey(User, unique=True)
from django.contrib.auth.models import User
class UserProfile(models.Model):
url = models.URLField()
home_address = models.TextField()
phone_numer = models.PhoneNumberField()
user = models.ForeignKey(User, unique=True)
(注意上面代码里用 ForeignKey 搭配 unique=True 的设置,取代了老的做法 OneToOneField)
2. 然后在 settings.py 里面设置一下,让 Django 的框架知道你定义了这个模型作为 profile:
AUTH_PROFILE_MODULE = 'myapp.UserProfile'
这一步需要特别当心,只需要写 appname.ModelName 即可。如果写多了,则可能出现 "too many values to unpack" 的错误。
3. 当然还需要执行一下同步数据库操作:
python manage.py syncdb
4. 现在可以利用内建的 User 模型的 get_profile 方法获取到对应的 Profile 对象:
from django.contrib.auth.models import User
u = User.objects.get(pk=1) # Get the first user in the system
user_address = u.get_profile().home_address
u = User.objects.get(pk=1) # Get the first user in the system
user_address = u.get_profile().home_address
- Django 中如何给 User 增加额外的 Profile 信息
- django附加额外信息到user
- windows2000server 系统中如何给user 用户增加关机权限
- Django设置User Profile
- 非profile方式扩展Django User Model
- Django 1.5.4 专题18 User Profile
- Django使用Profile扩展User模块
- 如何在eclipse中给tomcat增加JVM的heapsize
- 【Django】django中登录到accounts/profile/的解决方案
- 如何在运行时改变User Profile的Provider
- flash中如何给组件增加数据
- django给admin增加功能
- Django中User对象
- ASP.NET 2.0使用user profile储存信息前,如何部署aspnetdb.mdf和连接
- 在User Profile Service中配置AD的同步连接
- oracle中建视图额外增加列
- 在InfoPath中如何获取当前用户的信息(Profile)
- 如何让SQL的SELECT 查询结果额外增加自动递增序号
- [摘要] 如何用 FastCGI 运行 Django
- [转贴] 中药内外合治急慢性鼻窦炎
- Django newforms
- Django 中如何针对自定义的 View 做分页显示
- Django 添加用户注册功能
- Django 中如何给 User 增加额外的 Profile 信息
- Django 中自定义 tag 如何获取变量参数的值
- Django文档翻译:模型参考(Model Reference)
- 开始学习 limodou 的 Django step by step 了
- Karrigell 初次学习
- SubSonic 的字段名未转义问题修正
- Django 学习纪要(1) 模版语法
- 注意 JavaScript 中 RegExp 对象的 test 方法
- CodeProject 文章收藏 (2007-1-3)