SQLFORM.factory - is it possible to add multiple child objects to one parent in one view?

126 views
Skip to first unread message

memo

unread,
Mar 24, 2013, 12:20:46 PM3/24/13
to web...@googlegroups.com
Hi,
Here the codes. Is there a quick way add multiple child objects to a parent in one page?

db.define_table('t_invoices',
Field('f_customer', type='reference t_customers',
label=T('Customer')),
Field('f_date', type='date',
label=T('Date')),
Field('f_invoice_no', type='string',
label=T('Invoice No')),
Field('f_net', type='string',
label=T('Net')),
Field('f_tax', type='string',
label=T('Tax')),
Field('f_total', type='string',
label=T('Total')),
auth.signature,
format='%(f_customer)s %(f_date)d',
migrate=settings.migrate)

db.define_table('t_invoices_archive',db.t_invoices,Field('current_record','reference t_invoices',readable=False,writable=False))

########################################
db.define_table('t_invoice_details',
Field('f_invoice', type='reference t_invoices',requires=IS_NOT_EMPTY(),readable=False,writable=False,
label=T('Invoice')),
Field('f_description', type='string',
label=T('Description')),
Field('f_quantity', type='integer',
label=T('Quantity')),
Field('f_price', type='double',
label=T('Price')),
Field('f_total', type='double',compute=lambda r: r['f_quantity']*r['f_price'],writable=False,
label=T('Total')),
Field('f_tax_percentage', type='integer',
label=T('Tax Percentage')),
Field('f_tax', type='double',compute=lambda r: r['f_total']*r['f_tax_percentage']/100,writable=False,
label=T('Tax')),
Field('f_total_w_tax', type='double',compute=lambda r: r['f_total']+r['f_tax'],writable=False,
label=T('Total W Tax')),
auth.signature,
format='%(f_invoice)s',
migrate=settings.migrate)

db.t_invoice_details.f_tax_percentage.requires=IS_IN_SET(['1','8','18'])
db.define_table('t_invoice_details_archive',db.t_invoice_details,Field('current_record','reference t_invoice_details',readable=False,writable=False))



def newInvoice():
    form=SQLFORM.factory(db.t_invoices,db.t_invoice_details)
    if form.process().accepted:
        id=db.t_invoices.insert(**db.t_invoices._filter_fields(form.vars))
        form.vars.f_invoice=id
        id=db.t_invoice_details.insert(**db.t_invoice_details._filter_fields(form.vars))
        response.flash=T('Thanks for filling out the form')
    return dict(form=form)






Alan Etkin

unread,
Mar 24, 2013, 1:37:01 PM3/24/13
to web...@googlegroups.com
Hi,
Here the codes. Is there a quick way add multiple child objects to a parent in one page?

Which are the children and which are the parets?. It seems that you want to add many invoice details without requesting new form pages (within the same invoice page).

I think that it's possible to build a customized parent child manager with SQLFORM.smartgrid. However, it would require a page reload for each new child, AFAIK.

Another possible approach would be a combination of normal crud/SQLFORM form, a LOADed, self-submitted child form. and perhaps a component with a list of children and a callback bound to the child component submit event.

Ergun Pekuysal

unread,
Mar 24, 2013, 3:30:34 PM3/24/13
to web...@googlegroups.com
Thank you. Can u pls provide a sample code for this?

Sent from my iPhone
--
 
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/E8KnPq0ZGqM/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Alan Etkin

unread,
Mar 24, 2013, 4:03:31 PM3/24/13
to
Thank you. Can u pls provide a sample code for this?

Derived from the book example (at the end of the Chapter 7, in the smartgrid section)

@auth.requires_login():
def invoice():
constraints=dict(invoice=db.invoice.id==<the invoice id>,
details=db.details.invoice_id==<the invoice id>)

   grid = SQLFORM.smartgrid(db.invoice,
constraints=constraints,
linked_tables=['details']) return locals()
 
You'd need also a previous action to create the invoice or else use a one time form to create the invoice the first time the user visits the page.

EDIT: or better, create the invoice automatically when the user visits the page.

Ergun Pekuysal

unread,
Mar 24, 2013, 4:17:36 PM3/24/13
to web...@googlegroups.com
Thank you so much. Is it possible to create the invoice and the details on the same page? 

Sent from my iPhone

On 24 Mar 2013, at 22:02, Alan Etkin <spam...@gmail.com> wrote:

Thank you. Can u pls provide a sample code for this?
Derived from the book example (at the end of the Chapter 7, in the smartgrid section)

@auth.requires_login():
def invoice():
constraints=dict(invoice=db.invoice.id==<the invoice id>,
details=db.details.invoice_id==<the invoice id>)

   grid = SQLFORM.smartgrid(db.invoice,
constraints=constraints,
linked_tables=['details']) return locals()
 
You'd need also a previous action to create the invoice or else use a one time form to create the invoice the first time the user visits the page.

黄祥

unread,
Mar 25, 2013, 2:54:04 AM3/25/13
to web...@googlegroups.com
please check discussion on :

there are 2 example apps on that.
hope this can help.
Reply all
Reply to author
Forward
0 new messages