Re: Nested table-queries

57 views
Skip to first unread message

Jurgis Pralgauskis

unread,
Apr 14, 2013, 6:46:45 PM4/14/13
to web...@googlegroups.com
I had some simiar sounding issues + some solution 

but now I don't have time to examine your problem...

2013 m. balandis 14 d., sekmadienis 19:47:46 UTC+3, D. rašė:
Dear Community,

I just started with web2py and ran into some difficulties:

I would like to display a custom table with my data. For the value of a specific field in each row I need the corresponding information from another table.

This is what I have so far:

def get_extended_info_for_case(case_id):
    # DB-Query here to get the fields&values I need    
    return extended_info

def manage():
    ext_info = lambda row: get_extended_info_for_case(row.cases.id)
    db.cases.extended_info = Field.Virtual(ext_info)
    rows = db(db.cases).select()
    headers = ["ID", "A", "B", "C", "Extended Info"]
    fields = ["id", "a", "b", "c", "extended_info"]
    table = TABLE(THEAD(TR(*[B(header) for header in headers])),
                  TBODY(*[TR(*[TD(row[field]) for field in fields])
                        for row in rows]))

This works when the helper-function get_extended_info returns just a plain string. However, if I have it return a list of tuples that I can then use to dynamically create links within the virtualfield - one for each tuple - I'm lost.

For example: 
extended_info = [(1,"A1"), (223,"whatever")]

resulting entry in the corresponding virtualfield should be a concatenation of several links:
A(A1, _href=URL("edit", args=1)), A(whatever, _href=URL("edit", args=223))

How can I accomplish this? Can I integrate the query to the second databasetable into the first one? Or do I need nested lambdas? I have to admit that I don't fully understand the way lambdas and the nested table-construction work. Is there a way to display this in a more human-readable form? Or am I better of contructing my table in the view instead?

Thanks a lot.

Best regards,
Dan

D.

unread,
Apr 15, 2013, 5:20:44 AM4/15/13
to web...@googlegroups.com
I figured it out myself :-)

I just needed an inline-loop in my lambda-function:

ext_info = lambda row: [A(op, _href=URL("edit", args=op_id, user_signature=True)) + " "  for op, op_id in get_extended_info_for_case(row.cases.id)]



I find this very ugly though (lambdas and inline-loops in general) and it does not look pythonic. Is there any way to make it prettier?

Jurgis Pralgauskis

unread,
Apr 15, 2013, 4:08:13 PM4/15/13
to web...@googlegroups.com
I think you can have the same without lambda
 
def ext_info (row): 
   return [A(op, _href=URL("edit", args=op_id, user_signature=True)) + " "  for op, op_id in get_extended_info_for_case(row.cases.id)]


--
 
---
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/o6_wQZ_HBgM/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.
 
 



--
Jurgis Pralgauskis
tel: 8-616 77613;
Don't worry, be happy and make things better ;)
http://galvosukykla.lt
Reply all
Reply to author
Forward
0 new messages