Django使用Profile扩展User模块方式


Posted in Python onMay 14, 2020

首先创建Profile应用

python manage.py startapp profiles

profiles/models.py

# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save

class UserProfile(models.Model):
  user = models.OneToOneField(User)
  nickname = models.CharField(max_length=16, default='', blank=True)
  sex = models.IntegerField(default=0)
  phone = models.CharField(max_length=16, default='', blank=True)

  def __str__(self):
    return self.nickname

  def __unicode__(self):
    return self.nickname

def create_user_profile(sender, instance, created, **kwargs):
  if created:
    profile = UserProfile()
    profile.user = instance
    profile.save()

post_save.connect(create_user_profile, sender=User)

profiles/admin.py

# -*- coding: utf-8 -*-
from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from .models import UserProfile

class ProfileInline(admin.StackedInline):
  model = UserProfile
  max_num = 1
  can_delete = False

class UserProfileAdmin(UserAdmin):
  inlines = [ProfileInline, ]

admin.site.unregister(User)
admin.site.register(User, UserProfileAdmin)

settings.py

添加

AUTH_PROFILE_MODULE = 'profiles.UserProfile'

测试

$ python manage.py syncdb
$ python manage.py shell
>>> from django.contrib.auth.models import User
>>> user = User()
>>> user.username='testuser'
>>> user.save()
>>> User.objects.all()[0].userprofile

补充知识:django中登录到accounts/profile/的解决方案

Django使用Profile扩展User模块方式

在project的setting里加一句话就Okay!

LOGIN_REDIRECT_URL = '/index'

以上这篇Django使用Profile扩展User模块方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python Trie树实现字典排序
Mar 28 Python
python里大整数相乘相关技巧指南
Sep 12 Python
零基础写python爬虫之神器正则表达式
Nov 06 Python
Python的爬虫包Beautiful Soup中用正则表达式来搜索
Jan 20 Python
一些常用的Python爬虫技巧汇总
Sep 28 Python
Python使用剪切板的方法
Jun 06 Python
python 成功引入包但无法正常调用的解决
Mar 09 Python
Python figure参数及subplot子图绘制代码
Apr 18 Python
Django REST 异常处理详解
Jul 15 Python
如何基于matlab相机标定导出xml文件
Nov 02 Python
Python Pandas知识点之缺失值处理详解
May 11 Python
Python机器学习实战之k-近邻算法的实现
Nov 27 Python
python网络编程之五子棋游戏
May 14 #Python
Jupyter notebook如何修改平台字体
May 13 #Python
解决启动django,浏览器显示“服务器拒绝访问”的问题
May 13 #Python
Django 允许局域网中的机器访问你的主机操作
May 13 #Python
Django 用户登陆访问限制实例 @login_required
May 13 #Python
Python selenium模拟手动操作实现无人值守刷积分功能
May 13 #Python
PyQt5 控件字体样式等设置的实现
May 13 #Python
You might like
thinkphp常见路径用法分析
2014/12/02 PHP
php提示Failed to write session data错误的解决方法
2014/12/17 PHP
php实现每天自动变换随机问候语的方法
2015/05/12 PHP
thinkphp的dump函数无输出实例代码
2016/11/15 PHP
浅谈Laravel中的一个后期静态绑定
2017/08/11 PHP
PHP 判断字符串是中文还是英文, 或者是中英混合
2021/03/09 PHP
一段多浏览器的"复制到剪贴板"javascript代码
2007/03/27 Javascript
一些实用的jQuery代码片段收集
2011/07/12 Javascript
JavaScript函数的一些注意要点小结及js匿名函数
2015/11/10 Javascript
javascript HTML5文件上传FileReader API
2020/03/27 Javascript
集合Bootstrap自定义confirm提示效果
2017/09/19 Javascript
JS中精巧的自动柯里化实现方法
2017/12/12 Javascript
vue.js做一个简单的编辑菜谱功能
2018/05/08 Javascript
jQuery easyui datagird编辑行删除行功能的实现代码
2018/09/20 jQuery
如何在微信小程序中使用骨架屏的步骤
2020/06/12 Javascript
如何在postman中添加cookie信息步骤解析
2020/06/30 Javascript
微信小程序picker组件两列关联使用方式
2020/10/27 Javascript
python sys模块sys.path使用方法示例
2013/12/04 Python
Python正则表达式使用范例分享
2016/12/04 Python
python 随机森林算法及其优化详解
2019/07/11 Python
Python中正反斜杠(‘/’和‘\’)的意义与用法
2019/08/12 Python
python中enumerate() 与zip()函数的使用比较实例分析
2019/09/03 Python
python 如何将数据写入本地txt文本文件的实现方法
2019/09/11 Python
解决TensorFlow训练内存不断增长,进程被杀死问题
2020/02/05 Python
在Sublime Editor中配置Python环境的详细教程
2020/05/03 Python
.NET是怎么支持多种语言的
2015/02/24 面试题
初中生期末考试的自我评价
2013/12/17 职场文书
秋季运动会加油稿200字
2014/01/11 职场文书
三年大学自我鉴定
2014/01/16 职场文书
大学生联谊活动策划书(光棍节)
2014/10/10 职场文书
报名委托书
2015/01/29 职场文书
业务员年终工作总结2015
2015/05/28 职场文书
任长霞观后感
2015/06/16 职场文书
小学家庭教育心得体会
2016/01/14 职场文书
HTML中的表单Form实现居中效果
2021/05/25 HTML / CSS
Java实现简易的分词器功能
2021/06/15 Java/Android