Django使用Profile擴(kuò)展User模塊方式
首先創(chuàng)建Profile應(yīng)用
python manage.py startapp profiles
profiles/models.py
# -*- coding: utf-8 -*-from django.db import modelsfrom django.contrib.auth.models import Userfrom django.db.models.signals import post_saveclass 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.nicknamedef 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 adminfrom django.contrib.auth.models import Userfrom django.contrib.auth.admin import UserAdminfrom .models import UserProfileclass ProfileInline(admin.StackedInline): model = UserProfile max_num = 1 can_delete = Falseclass 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è)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. JavaScript實(shí)現(xiàn)留言板實(shí)戰(zhàn)案例2. vscode運(yùn)行php報(bào)錯(cuò)php?not?found解決辦法3. 使用Blazor框架實(shí)現(xiàn)在前端瀏覽器中導(dǎo)入和導(dǎo)出Excel4. Python基于requests庫(kù)爬取網(wǎng)站信息5. ASP基礎(chǔ)知識(shí)Command對(duì)象講解6. 資深程序員:給Python軟件開(kāi)發(fā)測(cè)試的25個(gè)忠告!7. 如何從Python的cmd中獲得.py文件參數(shù)8. 如何在python中執(zhí)行另一個(gè)py文件9. Python-openpyxl表格讀取寫(xiě)入的案例詳解10. python中文本字符處理的簡(jiǎn)單方法記錄

網(wǎng)公網(wǎng)安備