更改Userprofile

时间:2017-05-02 15:38:02

标签: python django

您好我有UserSettings和用户可以使用ProfilePicture。问题是用户无法更改他的任何个人资料设置。因为我得到错误Unique Constraint failed或(这就是我现在得到的)'int' object has no attribute '_committed'

我认为问题出在视图中,但我不知道如何改变它。

我的模特:

class UserSettings(models.Model):
profileimage = models.ImageField(verbose_name=_(u"Change Your Profilepicture"),upload_to=upload_location,
                    default=1, 
                    blank=True,
                    )

Info = models.CharField(verbose_name=_(u"Tell us about yourself"),max_length=500,default="I'm a Human")
status = 
City = models.CharField(verbose_name=_(u"Where are you from"),max_length=500,default='Earth')
user = models.OneToOneField(User, default=1)
objects = models.Manager()
class Meta:
    ordering =['-user']
def __unicode__(self):
    return self.user.username

我的观点:

@login_required
    def userprofiletwo(request):
    user = request.user
    form = UserSettingsForm(request.POST or None, request.FILES or None)
    if form.is_valid():
        user = request.user
        form.save()
        messages.success(request, 'Your personal Settings are Updated')
        return redirect('userprofiletwo')
    context = {
    'form':form,
    }
    return render(request, 'userprofile/userprofiletwo.html', context)

我尝试将模型上的OnetoOne字段更改为FeoreignKey或ManytoMany但这会产生新的用户配置文件,而不是用新信息替换旧信息。

谢谢你的建议。

1 个答案:

答案 0 :(得分:0)

您需要将当前个人资料传递到表单中。

form = UserSettingsForm(request.POST or None, request.FILES or None, instance=request.user.profile)