ringemup wrote:
> Hi folks,
> 
> I feel like I must be doing something stupid.  I've got the following
> view:
> 
> def adopt_bunny(request, id):
>       try:
>               bunny = Bunny.objects.get(pk=int(id))
>       except:
>               raise Http404
>       adoptions = bunny.adoption_count
>       adoptions = adoptions + 1
>       bunny.adoption_count = adoptions
>       bunny.save()
>       return single_bunny(request, id, True)
> 
> Every time I make this request (after the first time for that
> particular bunny), it increments by 3 or 4 instead of by 1.
> single_bunny() just calls the object_detail() generic view with some
> extra context.
> 
> How can I track down what's going on?  Or am I doing something
> completely braindead?
> 
> Thanks!

You don't need temp varible.  and there is a short cut for your 
try/except 
http://www.djangoproject.com/documentation/shortcuts/#get-object-or-404 
This would do

def adopt_bunny(request, id):
   bunny = get_object_or_404(Bunny, pk=id)
   bunny.adoption_count += 1
   bunny.save()

Don't know why it would inc by 3 or 4.  Assuming you are running dev 
server...  I'd add a print statement in there

   print "bunny %s: %s" % (bunny.id, bunny.adoption_count)

just to see when/if this code is being called multiple times and the 
value of adoption_count when it is.  Maybe add print to Bunny's save 
method to see where else it is getting modified/saved.

Also I'd probably make a method in the BunnyModel instead of doing this 
in the view.  Maybe overkill for this case (but today's overkill is 
tomorrow's maintenance saving feature)

Bunny(Model):
   ...
   def adopt(self):
       self.adoption_count += 1
       self.save()

Then from your view call it

def adopt_bunny(request, id):
   bunny = get_object_or_404(Bunny, pk=id)
   bunny.adopt()
   return single_bunny(request, id, True)



-- 
Norman J. Harman Jr.  512 912-5939
Technology Solutions Group, Austin American-Statesman
___________________________________________________________________________
Get out and about this spring with the Statesman! In print and online,
the Statesman has the area's Best Bets and recreation events.
Pick up your copy today or go to statesman.com 24/7.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to