我有一个更新表单,当我按下更新表单以更新用户配置文件时,我收到错误"' UserProfile'对象没有属性' id'"
我的模特是:
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name="custom_user_profile", primary_key=True)
organization = models.CharField(max_length=100, default='', blank=True)
address_line_1 = models.CharField(max_length=100, default='', blank=True)
address_line_2 = models.CharField(max_length=100, default='', blank=True)
city = models.CharField(max_length=100, default='', blank=True)
state = models.CharField(max_length=100, default='', blank=True)
post_code = models.CharField(max_length=100, default='', blank=True)
country = models.CharField(max_length=100, default='', blank=True)
def __unicode__(self):
return self.user.username
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
profile, new = UserProfile.objects.get_or_create(user=instance)
我的观点是:
from django.shortcuts import render_to_response
from django.shortcuts import HttpResponseRedirect
from django.template import RequestContext
from django.core.context_processors import csrf
from django.contrib.auth.decorators import login_required
from user_profile.models import UserProfile
from django.contrib.auth.models import User
from forms import UserProfileForm
from django.shortcuts import redirect
@login_required
def update_profile(request):
userProfile = UserProfile.objects.get(user=request.user)
form = UserProfileForm(initial={
'organization':userProfile.organization,
'address_line_1':userProfile.address_line_1,
'address_line_2':userProfile.address_line_2,
'city':userProfile.city,
'state':userProfile.state,
'post_code':userProfile.post_code,
'country':userProfile.country,
})
return render_to_response('user_profile/update_profile.html', {'form':form}, RequestContext(request))
@login_required
def profile(request, profile_id):
if profile_id == "0":
if request.user.is_authenticated:
userProfile = UserProfile.objects.get(user=request.user)
else:
userProfile = UserProfile.objects.get(pk=profile_id)
return render_to_response('user_profile/profile.html', {'userProfile':userProfile}, RequestContext(request))
@login_required
def send_update_profile(request):
if request.method == 'POST':
form = UserProfileForm(request.POST)
if form.is_valid():
userProfile = UserProfile.objects.get(user=request.user)
organization = form.cleaned_data['organization']
userProfile.organization = organization
address_line_1 = form.cleaned_data['address_line_1']
userProfile.address_line_1 = address_line_1
address_line_2 = form.cleaned_data['address_line_2']
userProfile.address_line_2 = address_line_2
city = form.cleaned_data['city']
userProfile.city = city
state = form.cleaned_data['state']
userProfile.state = state
post_code = form.cleaned_data['post_code']
userProfile.post_code = post_code
country = form.cleaned_data['country']
userProfile.country = country
userProfile.save()
return redirect('/user/profile/' + str(userProfile.id))
else:
form = UserProfileForm()
return redirect('/user/send_update_profile/')
我无法理解为什么。有一点是在admin.py中,如果我尝试添加' id'作为列表显示。发生错误,表示我正在创建的用户配置文件中没有id。虽然,我认为在创建模型时会自动创建一个id字段。
有什么建议吗?
答案 0 :(得分:1)
除非您自己指定主键,否则Django会为每个模型提供一个名为id
的{{3}}。在您的情况下,您的模型没有id
字段,因为您已将user
作为主键。
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name="custom_user_profile", primary_key=True)
您可以更改代码以使用userProfile.user_id
。另一种选择是使用automatic primary key field来访问主键。
return redirect('/user/profile/' + str(userProfile.pk))