Django使用Profile擴展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’
測試
$ 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’
以上這篇Django使用Profile擴展User模塊方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python實現(xiàn)讀取類別頻數(shù)數(shù)據(jù)畫水平條形圖案例2. python中PyQuery庫用法分享3. python操作數(shù)據(jù)庫獲取結(jié)果之fetchone和fetchall的區(qū)別說明4. PHP獲取時間戳等相關(guān)函數(shù)匯總5. JSP+Servlet實現(xiàn)文件上傳到服務(wù)器功能6. Ajax實現(xiàn)頁面無刷新留言效果7. php5.6不能擴展redis.so的解決方法8. python 爬取嗶哩嗶哩up主信息和投稿視頻9. CSS3實現(xiàn)動態(tài)翻牌效果 仿百度貼吧3D翻牌一次動畫特效10. AJAX實現(xiàn)數(shù)據(jù)的增刪改查操作詳解【java后臺】
