Django模型表单保存,'int'对象没有属性'save'

时间:2013-03-08 14:07:02

标签: python django

我有这个错误:

  

'int'对象没有属性'save'

class Profile(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField(max_length=255)
    tel = models.CharField(max_length=255)

views.py:

def add_profile(request):
    user = request.user.id
    form = ProfileForm()
    if request.method == "POST":
        form = ProfileForm(request.POST)
        if form.is_valid():
            user.save()
            form.save()
            return HttpResponseRedirect('/profile/')
    return render_to_response('profile.html',{'form':form},context_instance=RequestContext(request))

1 个答案:

答案 0 :(得分:2)

def add_profile(request):
    save_user = Profile(user=request.user)
    form = ProfileForm()
    if request.method == "POST":
        form = ProfileForm(request.POST, instance=save_user)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/profile/')
    return render_to_response('profile.html',{'form':form},context_instance=RequestContext(request))