Django使用Profile扩展User模块方式
作者:kongxx 发布时间:2023-06-16 12:26:12
标签:Django,Profile,User
首先创建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/的解决方案
在project的setting里加一句话就Okay!
LOGIN_REDIRECT_URL = '/index'
来源:https://blog.csdn.net/kongxx/article/details/50621745?
0
投稿
猜你喜欢
- PHP5.4后新增traits实现代码复用机制,Trait和类相似,但不能被实例化,无需继承,只需要在类中使用关键词use引入即可,可引入多
- 每一个网页项目都少不了导航栏,通过原始的方法基本上都是可以写出来的。但是要写出代码量少,冗余度低的代码就要动脑子思考一下了。最近写了一个百度
- 对于vue.js中的this.emit的理解:this.emit(‘increment1',”这个位子是可以加参数的”);其实它的作
- SQL*DBA命令的安全性: 如果您没有SQL*PLUS应用程序,您也可以使用SQL*DBA作SQL查权限相关的命令只能分配给Oracle软
- MySQL 提供了一个很有意思的Engine:Federated!如果你了解Linux下面的Link的话,就应该很好理解这个Federate
- 事情开始得很简单。MegaWare公司市场部门想要一个新的网站来发布文档,开发团队觉得使用SQL Server 2000数据库作为文档存储仓
- 一、闭包1.1 三要素 必须有一个内嵌函数内嵌函数必须引用外部函数中变量外部函数返回值必须是内嵌函数1.2 语法# 语法def 外部函数名(
- 目的:python能使用xlrd模块实现对Excel数据的读取,且按照想要的输出形式。总体思路:(1)要想实现对Excel数据的读取,需要用
- 并发是一个很酷的话题,一旦你掌握了它,就会成为一笔巨大的财富。说实话,我一开始很害怕写这篇文章,因为我自己直到最近才对并发性不太适应。我已经
- 一、更改my.cnf配置文件1.用命令编辑/etc/my.cnf配置文件,即:vim /etc/my.cnf 或者 vi /etc/my.c
- 方式一: os.fork()# -*- coding:utf-8 -*-"""pid=os.fork() &n
- 本文实例为大家分享了python K均值聚类的具体代码,供大家参考,具体内容如下#-*- coding:utf-8 -*- #!/usr/b
- 因为最近接触到调用新浪微博开放接口的项目,所以就想试试用python调用微博API。SDK下载地址:http://open.weibo.co
- 为什么使用虚拟环境因为直接在真实环境进行安装python的包会造成环境之间的污染,因此需要创建虚拟环境,原则上每一个项目都需要有一个独属于自
- import time# time模块中包含了许多与时间相关的模块,其中通过time()函数可以获取当前的时间。count = 100pri
- 本文实例讲述了python实现的简单抽奖系统。分享给大家供大家参考。具体实现方法如下:#!/usr/bin/env python#codin
- MaxDB和MySQL是独立的数据库管理服务器。系统间的协同性是可能的,通过相应的方式,系统能够彼此交换数据。要想在MaxDB和MySQL之
- 一、什么是上下文管理器我们在处理文件的时候经常看到下面这样的代码,它即是上下文管理器:with open('test.txt'
- 很久之前,分享过一次Python代码实现验证码识别的办法。当时采用的是pillow+pytesseract,优点是免费,较为易用。但其识别精
- 使用触发器触发器发生什么事情之后或之前,会自动执行某条语句,这就是触发器创建触发器创建触发器要给出的4条关键信息:1.唯一的触发器名2.触发