欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Django使用Profile擴(kuò)展User模塊方式

 更新時(shí)間:2020年05月14日 08:48:29   作者:kongxx  
這篇文章主要介紹了Django使用Profile擴(kuò)展User模塊方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧

首先創(chuàng)建Profile應(yīng)用

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'

測(cè)試

$ 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

補(bǔ)充知識(shí):django中登錄到accounts/profile/的解決方案

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

LOGIN_REDIRECT_URL = '/index'

以上這篇Django使用Profile擴(kuò)展User模塊方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python IDLE或shell中切換路徑的操作

    Python IDLE或shell中切換路徑的操作

    這篇文章主要介紹了Python IDLE或shell中切換路徑的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2020-03-03
  • python創(chuàng)建學(xué)生管理系統(tǒng)

    python創(chuàng)建學(xué)生管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了python創(chuàng)建學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • tensorflow模型轉(zhuǎn)ncnn的操作方式

    tensorflow模型轉(zhuǎn)ncnn的操作方式

    這篇文章主要介紹了tensorflow模型轉(zhuǎn)ncnn的操作方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2020-05-05
  • pycharm安裝中文插件的2種方法圖文詳解

    pycharm安裝中文插件的2種方法圖文詳解

    PyCharm可以說(shuō)是當(dāng)今最流行的一款Python?IDE了,下面這篇文章主要給大家介紹了關(guān)于pycharm安裝中文插件的2種方法,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-06-06
  • Python中相見恨晚的技巧(記得收藏)

    Python中相見恨晚的技巧(記得收藏)

    這篇文章主要介紹了一些Python中相見恨晚的使用技巧,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-04-04
  • python顯示生日是星期幾的方法

    python顯示生日是星期幾的方法

    這篇文章主要介紹了python顯示生日是星期幾的方法,涉及Python使用date模塊操作日期的技巧,需要的朋友可以參考下
    2015-05-05
  • Python編寫一個(gè)驗(yàn)證碼圖片數(shù)據(jù)標(biāo)注GUI程序附源碼

    Python編寫一個(gè)驗(yàn)證碼圖片數(shù)據(jù)標(biāo)注GUI程序附源碼

    這篇文章主要介紹了Python編寫一個(gè)驗(yàn)證碼圖片數(shù)據(jù)標(biāo)注GUI程序,本文給大家附上小編精心整理的源碼,需要的朋友可以參考下
    2019-12-12
  • python實(shí)現(xiàn)LRU熱點(diǎn)緩存及原理

    python實(shí)現(xiàn)LRU熱點(diǎn)緩存及原理

    LRU算法根據(jù)數(shù)據(jù)的歷史訪問記錄來(lái)進(jìn)行淘汰數(shù)據(jù),其核心思想是“如果數(shù)據(jù)最近被訪問過,那么將來(lái)被訪問的幾率也更高”。 。這篇文章主要介紹了python實(shí)現(xiàn)LRU熱點(diǎn)緩存,需要的朋友可以參考下
    2019-10-10
  • Python Numpy之linspace用法說(shuō)明

    Python Numpy之linspace用法說(shuō)明

    這篇文章主要介紹了Python Numpy之linspace用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2021-04-04
  • Python之string編碼問題

    Python之string編碼問題

    這篇文章主要介紹了Python之string編碼問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02

最新評(píng)論