Django使用Profile扩展User模块方式
首先创建Profile应用
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模块方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持好吧啦网。
相关文章:
1. Admin - SpringBoot + Maven 多启动环境配置实例详解2. Android WebView通过动态的修改js去拦截post请求参数实例3. SpringBoot2整合Ehcache组件实现轻量级缓存管理4. spring boot集成mongodb的增删改查的示例代码5. Django def clean()函数对表单中的数据进行验证操作6. 如何用python写个模板引擎7. django中嵌套的try-except实例8. 浅谈vue实现双向事件绑定v-model的原理9. Android自定义View实现气泡动画10. Django结合使用Scrapy爬取数据入库的方法示例