2016-12-03 14 views
0

私は更新フォームを持っていますが、ユーザープロファイルを更新するためにフォームの更新が更新されました。 '' UserProfile 'オブジェクトには' id '属性がありません。 "'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/') 

私がなぜうまくいかないことができます。 1つはadmin.pyで、リスト表示として 'id'を追加しようとした場合です。作成中のユーザープロファイルにIDが存在しないというエラーが発生します。しかし、私はモデルが作成されたときにidフィールドが自動的に作成されると考えました。

提案がありますか?

+0

通常、ユーザーをプライマリキーとして定義することで、それをオーバーライドしました。 –

答えて

1

あなたが主キーを自分で指定しない限り、Djangoは各モデルにidというautomatic primary key fieldという名前を付けます。あなたの場合、userを主キーにしているため、モデルにidフィールドがありません。

class UserProfile(models.Model): 
    user = models.OneToOneField(User, related_name="custom_user_profile", primary_key=True) 

userProfile.user_idを使用するようにコードを変更することができます。別のオプションは、pk shortcutを使用して主キーにアクセスすることです。

return redirect('/user/profile/' + str(userProfile.pk)) 
+0

それに感謝します。 – keno

関連する問題