django1.8 增加注册用户其他字段
2016-03-09 13:54
447 查看
在V1.6及之后版本已经删除get_profile()方法,需要使用userprofile。
1.新建moduel,名为UserProfile:
2.修改setting.py,添加字段:
3.使用 python manage.py migrate; python manage.py makemigrations; python manage.py migrate新建UserProfile表
4.为UserProfile表的test用户设字段值(phone,addr):
5.获取test用户的phone值:
1.新建moduel,名为UserProfile:
class UserProfile(models.Model): user=models.OneToOneField(User,unique=True) #不使用ForginKey phone=models.CharField(max_length=20) addr=models.CharField(max_length=500)
2.修改setting.py,添加字段:
AUTH_PROFILE_MODULE = 'sinfors.UserProfile' #app.moduelname
3.使用 python manage.py migrate; python manage.py makemigrations; python manage.py migrate新建UserProfile表
4.为UserProfile表的test用户设字段值(phone,addr):
u = User.objects.get(username='test') UserProfile.objects.create(user=u,phone='01011111111',addr='BJ') #不需要save
5.获取test用户的phone值:
print request.user.userprofile.phone #如果该用户的userprofile值还未添加,则会提示“User has no userprofile”
相关文章推荐
- 李世石和AlphaGo人机围棋大赛
- Go语言中Restful模式与Routes路由框架
- Google 搜索命令
- CodeForces 630D-Hexagons!
- goto void
- Django入门
- HDU 3903 Trigonometric Function(数学定理)
- HDU 3903 Trigonometric Function(数学定理)
- 我与小娜(29):AlphaGo必胜!
- 谷歌出品!7条超实用的虚拟现实交互式设计原则
- 编写GO的WEB开发框架 (八): Session支持及自定义Session
- Tushare + pyalgotrade + pandas 环境搭建(MAC环境)
- 【POJ2195】Going Home(费用流)
- Django: 之Web框架完美解析
- google-benchmark 渐进学习
- 15款Django开发常用软件包
- 五. Django的forms与ModelForm
- 简化MongoAPI第二部分--MongoTemplate
- Django请求响应对象
- Django控制器