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 + hadoop streaming 分布式编程(一) -- 原理介绍,样例程序与本地调试
Jul 14 Python
Python pickle模块用法实例分析
May 27 Python
Python单例模式实例详解
Mar 01 Python
Python入门_浅谈for循环、while循环
May 16 Python
使用python实现抓取腾讯视频所有电影的爬虫
Apr 15 Python
django如何实现视图重定向
Jul 24 Python
使用Python来做一个屏幕录制工具的操作代码
Jan 18 Python
Django中FilePathField字段的用法
May 21 Python
keras训练浅层卷积网络并保存和加载模型实例
Jul 02 Python
keras的三种模型实现与区别说明
Jul 03 Python
Python爬虫之Selenium实现关闭浏览器
Dec 04 Python
Python实现数据的序列化操作详解
Jul 07 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
其他功能
2006/10/09 PHP
php 计算两个时间戳相隔的时间的函数(小时)
2009/12/18 PHP
Linux下CoreSeek及PHP扩展模块的安装
2012/09/23 PHP
解析php中heredoc的使用方法
2013/06/17 PHP
Yii2框架实现登录、退出及自动登录功能的方法详解
2017/10/24 PHP
PHP中危险的file_put_contents函数详解
2017/11/04 PHP
javascipt匹配单行和多行注释的正则表达式
2013/11/20 Javascript
JavaScript charCodeAt方法入门实例(用于取得指定位置字符的Unicode编码)
2014/10/17 Javascript
jQuery插件编写步骤详解
2016/06/03 Javascript
Node.js 的模块知识汇总
2017/08/16 Javascript
JS倒计时实例_天时分秒
2017/08/22 Javascript
使用JS中的Replace()方法遇到的问题小结
2017/10/20 Javascript
JavaScript树的深度优先遍历和广度优先遍历算法示例
2018/07/30 Javascript
vue2.0 下拉框默认标题设置方法
2018/08/22 Javascript
jquery插件开发模式实例详解
2019/07/20 jQuery
Vue自定义组件的四种方式示例详解
2020/02/28 Javascript
Vue——前端生成二维码的示例
2020/12/19 Vue.js
[55:03]完美世界DOTA2联赛PWL S2 LBZS vs FTD.C 第二场 11.20
2020/11/20 DOTA
[09:59]DOTA2-DPC中国联赛2月7日Recap集锦
2021/03/11 DOTA
Python显示进度条的方法
2014/09/20 Python
把项目从Python2.x移植到Python3.x的经验总结
2015/04/20 Python
python 链接和操作 memcache方法
2017/03/04 Python
关于python pyqt5安装失败问题的解决方法
2017/08/08 Python
python3调用R的示例代码
2018/02/23 Python
python3.X 抓取火车票信息【修正版】
2018/06/19 Python
用Python实现筛选文件脚本的方法
2018/10/27 Python
python 实现倒排索引的方法
2018/12/25 Python
Python中的支持向量机SVM的使用(附实例代码)
2019/06/26 Python
用python实现前向分词最大匹配算法的示例代码
2020/08/06 Python
Selenium执行完毕未关闭chromedriver/geckodriver进程的解决办法(java版+python版)
2020/12/07 Python
党员学习十八大感想
2014/01/17 职场文书
学校政风行风自查自纠报告
2014/10/21 职场文书
领导干部学习心得体会
2016/01/23 职场文书
创业计划书之烤红薯
2019/09/26 职场文书
jquery插件实现图片悬浮
2021/04/16 jQuery
浅谈MySQL user权限表
2021/06/18 MySQL