views:

32

answers:

2

hi can anybody gimme any help about how to overwrite a model entry. i have latitude, longitude, and status fields in my model. once i save the data i cant update or change it from my custom template. how to update or overwrite these fields. this is 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}) 

and this is my Forms.py

class registerForm(forms.ModelForm):
     class Meta:
     model=register
     fields = ('latitude', 'longitude', 'status')
A: 

Assigning a PK already in use in the database will overwrite that row completely when the model is saved. Or you can retrieve a model from the database, change the fields other than the PK, and save it.

Ignacio Vazquez-Abrams
sorry i am not understanding exactly what you are saying can you please give me an example
MajorGeek
A: 

this line solved my problem

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