def view_invoices():customer_id = request.args(0)if customer_id:invoice_query=db.invoice.customer==customer_idelse:invoice_query=db.invoice.customer!=Noneconstraints=dict(invoice=invoice_query)db.item.invoice.writable=False #avoid invoice # from any particular row in item table get overwritteninvoice_grid = SQLFORM.smartgrid(db.invoice, constraints=constraints)return locals()
SQLFORM.smartgrid(..., args=request.args[:1])Thanks Anthony for your explanation of request.args use with smartgrid, I tried your suggestion, but had the same error. I think i made a mistake in the constraints in my earlier post (according to the documentation, constraints is a dict of tablename:query), here is what i have changed.invoice_grid = SQLFORM.smartgrid(db.invoice, args=request.args[:1], constraints=dict(invoice=invoice_query), linked_tables=dict(invoice=['item'], item=[]))Noted that i could select a set of invoices based on the customer_id I passed in through request.args(0), the only thing it broke was when I clicked on the linked_tables "Items" with error Query Not Supported: invalid literal for long() with base 10: 'item'.I have been stuck here for a few days, any further help is much appreciated.
invoice_grid = SQLFORM.smartgrid(db.invoice, args=request.args[:1], constraints=dict(invoice=invoice_query), linked_tables=dict(invoice=['item'], item=[]))Noted that i could select a set of invoices based on the customer_id I passed in through request.args(0), the only thing it broke was when I clicked on the linked_tables "Items" with error Query Not Supported: invalid literal for long() with base 10: 'item'.I have been stuck here for a few days, any further help is much appreciated.
From the error message, I suspect the part linked_tables=dict(invoice=['item'],...) needs to specify an ID, and you've provided a string (and a string not corresponding to a valid base10 number).
here is the minimal app, and I did reproduce the problem and narrowed where it broke.In view_invoices() action, it checks if customer id args is provided, if so it will take it in for invoice_query = db.invoice.customer==customer_id, if not invoice_query = db.invoice.customer != None (pls let me know if there is a better way to define the query for selecting all invoice). Below code works when I specify the customer id in the URL eg. http://localhost:8000/tests/default/view_invoices/2, now when i click on the linked_tables 'Items', it will redirect to http://localhost:8000/tests/default/view_invoices/2/invoice/item.invoice/1.Now, if I don't specify the customer id in URL
Another question related to it is - how does web2py construct the URL for redirect when linked_tables is used? eg. http://localhost:8000/tests/default/view_invoices/2/invoice/item.invoice/1. The blue part is what I typed in, the red is provided by the linked_tables.
Pardon me for asking stupid question, what does SQLFORM.smartgrid(....., args=request.args[:1], ...) really do? you mentioned in earlier post that args must be a list, and it preserves the 1st argument of URL for application specific use. Does it tell web2py whatever url it creates, it should always preserve the same URL up to the 1st argument (for this case) eg. http://localhost/tests/default/view_invoices/2?
I looked up the documentation, there is little info about args in grid / smartgrid signature. If above assumption is correct, if my applications uses first 2 args for specific application purpose, i should modify the code to SQLFORM.smartgrid(....., args=request.args[:2], ...)?
Another stupid question - what's difference between request.args[:1] and request.args(0)?
def view_invoices():customer_id = request.args(0)if customer_id:
customer_id = request.args(0, cast=int, otherwise=lambda: None)
if customer_id is not None: