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中pycurl库的用法实例
Sep 30 Python
10款最好的Web开发的 Python 框架
Mar 18 Python
在Python中使用SQLite的简单教程
Apr 29 Python
举例讲解Python中的身份运算符的使用方法
Oct 13 Python
node.js获取参数的常用方法(总结)
May 29 Python
python用户管理系统
Mar 13 Python
机器学习之KNN算法原理及Python实现方法详解
Jul 09 Python
python3+PyQt5 实现Rich文本的行编辑方法
Jun 17 Python
PyQT5 emit 和 connect的用法详解
Dec 13 Python
pytorch之inception_v3的实现案例
Jan 06 Python
详解如何修改python中字典的键和值
Sep 29 Python
scrapy头部修改的方法详解
Dec 06 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
PHP反向代理类代码
2014/08/15 PHP
简单谈谈 php 文件锁
2017/02/19 PHP
javascript使用eval或者new Function进行语法检查
2010/10/16 Javascript
js/jquery获取文本框输入焦点的方法
2014/03/04 Javascript
基于jQuery全屏焦点图左右切换插件responsiveslides
2015/09/07 Javascript
Javascript 对cookie操作详解及实例
2016/12/29 Javascript
Vue数据驱动模拟实现5
2017/01/13 Javascript
JS判断微信扫码的方法
2017/08/07 Javascript
JS获取子节点、父节点和兄弟节点的方法实例总结
2018/07/06 Javascript
React+Webpack快速上手指南(小结)
2018/08/15 Javascript
使用 Node.js 实现图片的动态裁切及算法实例代码详解
2018/09/29 Javascript
Vue编程式跳转的实例代码详解
2019/07/10 Javascript
使用vue制作滑动标签
2019/09/21 Javascript
JavaScript中的几种继承方法示例
2020/12/06 Javascript
Python排序搜索基本算法之冒泡排序实例分析
2017/12/09 Python
python excel使用xlutils类库实现追加写功能的方法
2018/05/02 Python
python中的不可变数据类型与可变数据类型详解
2018/09/16 Python
获取Pytorch中间某一层权重或者特征的例子
2019/08/17 Python
pyqt5 QScrollArea设置在自定义侧(任何位置)
2019/09/25 Python
keras中的卷积层&池化层的用法
2020/05/22 Python
如何在python中判断变量的类型
2020/07/29 Python
如何从csv文件构建Tensorflow的数据集
2020/09/21 Python
Python 实现进度条的六种方式
2021/01/06 Python
用HTML5 Canvas API中的clearRect()方法实现橡皮擦功能
2016/03/15 HTML / CSS
html5声频audio和视频video等新特性详细说明
2012/12/26 HTML / CSS
Hunter Boots美国官方网站:赫特威灵顿雨靴
2018/06/16 全球购物
汽修专业学生自我鉴定
2013/11/16 职场文书
一月红领巾广播稿
2014/02/11 职场文书
2014全国两会大学生学习心得体会
2014/03/10 职场文书
党的群众路线教育实践活动对照检查材料范文
2014/09/24 职场文书
销售经理助理岗位职责
2015/04/13 职场文书
2016年“5.12”护士节慰问信
2015/11/30 职场文书
我的暑假生活作文(五年级)范文
2019/08/07 职场文书
mybatis使用oracle进行添加数据的方法
2021/04/27 Oracle
原生JS中应该禁止出现的写法
2021/05/05 Javascript
Mysql 8.x 创建用户以及授予权限的操作记录
2022/04/18 MySQL