django扩展User模型(model),profile
Posted gange111
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django扩展User模型(model),profile相关的知识,希望对你有一定的参考价值。
from django.contrib.auth.models import User # Create your models here. class Profile(models.Model): user = models.OneToOneField(User, related_name=‘profile‘) date_of_birth = models.DateTimeField(blank=True, null=True) photo = models.ImageField(upload_to=‘user/%Y/%m/%d‘, blank=True) def __str__(self): return ‘Profile for user {}‘.format(self.user.username)
修改setting.py
AUTH_PROFILE_MODULE = ‘account.Profile‘ # app名.model名
按照下面方式调用
@login_required def edit(request): if request.method == ‘POST‘: user_form = UserEditForm(instance=request.user, data=request.POST) profile_form = ProfileEditForm(instance=request.user.profile, data=request.POST, files=request.FILES) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() else: user_form = UserEditForm(instance=request.user) profile_form = ProfileEditForm(instance=request.user.profile) return render(request, ‘account/edit.html‘, {‘user_form‘: user_form, ‘profile_form‘: profile_form})
以上是关于django扩展User模型(model),profile的主要内容,如果未能解决你的问题,请参考以下文章