开发者

user_id is not unique

i have a this thing in my views,py

def status_change(request):
if request.method == "POST":
    rform = registerForm(data = request.POST)
    if rform.is_valid():
        register = rform.save(commit=False)
        register开发者_JS百科.user = request.user
            register.save()
                return render_to_response('home.html')
else:
    rform = registerForm() 
return render_to_response('status_change.html',{'rform':rform}) 

when i tried to save the fields for the second time in the model it says

"column user_id is not unique" actually i want to update it i tried the rform.save(force_update = True) but it didnt work how to solve this thing.


Every time when you save form, django creates new object. If you need to change(not create new) some object, you need first get object and then create form with instance of this object:

myobject = ...objects.get(....)
mform = MyForm(instance=myobject)


problem is solved

def status_change(request):
instance = get_object_or_404(register,pk=request.user.id)
 #rest of the code here
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜