简体   繁体   中英

Django - extend user profile error user has no attribute userprofile

Im following this link here https://simpleisbetterthancomplex.com/tutorial/2016/07/22/how-to-extend-django-user-model.html#onetoone

I didnt need the user section only the profile. the error im currently getting is

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

Which i understand is because the user model does not have a UserProfile. Im not sure how to get the UserProfile into the request so the form will work?

model

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

You need to get userprofile by attribute name, not the model name:

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

This might help, also u can set related_name argument, when creating a field, so the attribute name will correspond to that name.

Use primary_key of UserProfile class then it will work. Don't use UserProfile class directly in request.user

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM