views:

32

answers:

2

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.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.

A: 

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)
Saff
i tried in this way but it says "local variable 'register' referenced before assignment"where register is a class in my models.py file
MajorGeek
Can you post code???
Saff
A: 

problem is solved

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