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})