【问题标题】:Django - extend user profile error user has no attribute userprofileDjango - 扩展用户配置文件错误用户没有属性 userprofile
【发布时间】:2016-12-07 17:52:26
【问题描述】:

我在这里关注这个链接https://simpleisbetterthancomplex.com/tutorial/2016/07/22/how-to-extend-django-user-model.html#onetoone

我不需要用户部分,只需要配置文件。我目前得到的错误是

'User' object has no attribute 'UserProfile'
line 45: profile_form = ProfileForm(instance=request.user.UserProfile) 

我的理解是因为用户模型没有 UserProfile。 我不确定如何将 UserProfile 放入请求中以便表单可以正常工作?

型号

from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings
# Create your models here.

class UserProfile(models.Model):
    mpls_m_subscriptions = models.CharField(max_length=50,verbose_name="MPLS Maintenance Subscription",choices=settings.SUBSCRIPTION_TYPE,blank=True,null=True)
    user = models.OneToOneField(User, on_delete=models.CASCADE)

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.UserProfile.save()

forms.py

from django import forms
from home.models import UserProfile

class ProfileForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ('mpls_m_subscriptions',)

views.py

from django.shortcuts import get_object_or_404, render, render_to_response
from django.contrib.auth.decorators import login_required
from django.db import transaction
from django.http import HttpResponse
from home.forms import ProfileForm
@login_required
@transaction.atomic
def update_profile(request):
    if request.method == 'POST':
        profile_form = ProfileForm(request.POST, instance=request.user.UserProfile)
        if profile_form.is_valid():
            profile_form.save()
            messages.success(request, _('Your profile was successfully updated!'))
            return redirect('settings:profile')
        else:
            messages.error(request, _('Please correct the error below.'))
    else:
        profile_form = ProfileForm(instance=request.user.UserProfile)
    return render(request, 'home/profile.html', {
        'profile_form': profile_form
    })    

【问题讨论】:

  • 改用request.user.userprofile。自动生成的相关字段名称被 Django 转换为小写。

标签: python django


【解决方案1】:

您需要通过属性名称而不是模型名称获取用户配置文件:

...
else:
    profile_form = ProfileForm(instance=request.user.userprofile)

这可能会有所帮助,您也可以在创建字段时设置related_name 参数,因此属性名称将对应于该名称。

【讨论】:

    【解决方案2】:

    使用 UserProfile 类的 primary_key 就可以了。不要在 request.user 中直接使用 UserProfile 类

    profile_form = ProfileForm(instance=request.user.user_profile_primary_key)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-26
      • 2017-01-25
      • 2023-03-22
      • 2012-03-11
      • 1970-01-01
      • 2017-07-14
      • 2015-07-08
      • 1970-01-01
      相关资源
      最近更新 更多