Thanks for the tip about database sum. I've also moved most of the logic into the controller.
However, your suggestion that the database sum should occur *after* the SQLFORM.grid is incorrect. I tried it. The problem seems to be that the page is not refreshed after a *delete*, whereas the page *is* refreshed after an *update*. I surmise the reason is because when you do an update, you're brought to another page with the update form, and consequently, once you're finished with the form submission, the previous page is refreshed. But when you do a delete, you stay on the current page. You get a pop-up asking for delete confirmation, but otherwise there is no reason for the page to refresh. This may also explain why the label "xx records found" from SQLFORM.grid does not change; "xx" is unchanged after a delete. I believe this delete behaviour is wrong. So the question is: How do you force a page refresh after a delete? Thanks. On Saturday, 25 January 2014 08:50:10 UTC-5, Anthony wrote: > > The line in question comes *before* SQLFORM.grid is called (which is > where the delete happens), so the delete hasn't yet happened at that point. > > A couple other points: > > - You should really keep your code in the controller and limit your > view file to display logic. > - You can have the database calculate the sum of a numeric field for > you rather than iterating over records in Python. See > > http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#sum,-avg,-min,-max-and-len > . > > Anthony > > On Saturday, January 25, 2014 7:17:44 AM UTC-5, horridohobbyist wrote: >> >> Okay, I know the problem now. After a delete is performed, the line >> indicated by the comment below still thinks that the record is there, even >> though the record is verifiably absent when I check in Database >> Administration! It almost looks like a caching problem. I tried a >> db.commit() before the comment line, but it made no difference. >> >> {{ >> import locale >> locale.setlocale(locale.LC_ALL,'en_CA.utf8') >> >> total = 0 >> # the following line does not recognize the deletion >> rows = db(db.cart.buyer_id==auth.user_id).select() >> for row in rows: >> total += row.subtotal >> pass >> }} >> >> {{=SQLFORM.grid((db.cart.buyer_id==auth.user_id), >> maxtextlength=1000, >> paginate=10, >> searchable=False, >> details=False, >> editable=True, >> deletable=True, >> create=False, >> csv=False) >> }} >> >> <div style="text-align:right;font-size:large">Total: >> {{='C'+locale.currency(total,grouping=True)}}</div> >> >> >> >> >> On Saturday, 25 January 2014 05:59:06 UTC-5, horridohobbyist wrote: >>> >>> Never mind my last message. I'm still not sure it's being called. I >>> need to investigate further. >>> >>> On Saturday, 25 January 2014 05:47:35 UTC-5, horridohobbyist wrote: >>>> >>>> Oh, it's called BEFORE the delete! That's why I was getting unexpected >>>> results. >>>> >>>> I need to perform an action AFTER the delete. Is there a workaround? >>>> >>>> Thanks. >>>> >>>> On Saturday, 25 January 2014 01:00:25 UTC-5, Anthony wrote: >>>>> >>>>> Does the record actually get deleted from the database? If so, >>>>> ondelete should be getting called right before the delete. You may need >>>>> to >>>>> show some code and explain what you're seeing and what you expect. Also, >>>>> note that the ondelete code in .grid has changed in more recent versions, >>>>> but should have worked in 2.4.2. >>>>> >>>>> Anthony >>>>> >>>>> On Friday, January 24, 2014 8:22:17 PM UTC-5, horridohobbyist wrote: >>>>>> >>>>>> I'm using Version 2.4.2. >>>>>> >>>>>> I'm finding that the ondelete function is never called when I delete >>>>>> a record in SQLFORM.grid. Is this a bug, or am I doing something wrong? >>>>>> The >>>>>> onupdate function does get called when I update a record. >>>>>> >>>>>> Thanks. >>>>>> >>>>> -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.