How to structure a represent lambda for a non-primary field?

490 views
Skip to first unread message

Alex Glaros

unread,
Apr 20, 2013, 12:34:57 AM4/20/13
to
What is the represent feature syntax for having countryName appear in place of countryTelephoneCode using the represent feature?

Notice in the below situation that countryTelephoneCode is not the primary key of the Country table.

db.define_table('Party', ## super-type for people and organizations
Field('partyTypeID','reference PartyType'),
Field('displayName','string'))


db
.define_table('Country',
Field('countryName','string'),
Field('countryCode','string'),
Field('countryTelephoneCode','integer'))


db
.define_table('PartyPhoneNumberIntersection',
Field('partyID','reference Party'),
Field('countryTelephoneCode','integer'),
Field('telephoneNumber','integer'))


db
.PartyPhoneNumberIntersection.countryTelephoneCode.represent = lambda id,row: db.Country(countryTelephoneCode).countryName  ## this generates an error:  "global name 'countryTelephoneCode' is not defined" How can I fix this represent syntax?


Thanks,

Alex Glaros

Anthony

unread,
Apr 20, 2013, 12:47:55 AM4/20/13
to web...@googlegroups.com
Notice that the current row is passed into the lambda, and countryTelephoneCode is a field in that row, so you have to refer to it as row.countryTelephoneCode:

db.PartyPhoneNumberIntersection.countryTelephoneCode.represent = lambda id,row: db.Country(row.countryTelephoneCode).countryName

Anthony

On Saturday, April 20, 2013 12:23:50 AM UTC-4, Alex Glaros wrote:
What is the represent feature syntax for having countryName appear in place of countryTelephoneCode using the represent feature?

Notice in the above situation that countryTelephoneCode is not the primary key of the Country table.

Alex Glaros

unread,
Apr 20, 2013, 12:58:59 AM4/20/13
to web...@googlegroups.com
it looks good Anthony but this error is raised:

<type 'exceptions.AttributeError'> 'NoneType' object has no attribute 'countryName'


thanks,

Alex

Anthony

unread,
Apr 20, 2013, 1:13:19 AM4/20/13
to web...@googlegroups.com
Oh, yeah, forgot the code is not the id field of the Country table -- you need a full query:

db.PartyPhoneNumberIntersection.countryTelephoneCode.represent = lambda id,row:
    db
(db.Country.countryTelephoneCode == row.countryTelephoneCode).select().first().countryName

Anthony

Anthony

unread,
Apr 20, 2013, 1:15:59 AM4/20/13
to web...@googlegroups.com
Or slightly shorter:

db.PartyPhoneNumberIntersection.countryTelephoneCode.represent = lambda id,row:
    db
.Country(countryTelephoneCode=row.countryTelephoneCode).countryName

Anthony

Alex Glaros

unread,
Apr 20, 2013, 1:38:33 AM4/20/13
to web...@googlegroups.com
They both worked beautifully, thanks Anthony.

(Note to anyone copying this, Google Groups formatted a hard return that should be removed to make Anthony's examples work).

Alex

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

Reply all
Reply to author
Forward
0 new messages