Reputation: 726
What I want to do is simple I want to update the count variable of the object every time my view function is called.
My models is like this:
class Url(models.Model):
#some vars here
count=0
def __unicode__(self):
return self.urlx
def incr(self):
self.count+=1
and my view code is like this
@transaction.autocommit
def redirect(request,key):
if(key):
key='/'+key
try:
ob=Url.objects.get(urlx=key)
ob.incr() #not working
ob.save() #not working
return HttpResponseRedirect(ob.url)
val=ob.count
except Url.DoesNotExist:
key="Sorry! couldn't find that url"
return render_to_response('redir.html',{},context_instance=RequestContext(request))
I am sure I am overlooking something here, or is this not the correct way to do this?
Upvotes: 0
Views: 488
Reputation: 599580
Since you've missed out the "other vars here", it's not clear if you realize that Django model fields need to be, well, fields - that is, models.CharField
, models.IntegerField
, etc. Putting count=0
just makes a class variable that won't be persisted in the database. You probably just want count = models.IntegerField(default=0)
.
Upvotes: 3