DAL format and represent outside of SQLFORM

93 views
Skip to first unread message

Jim S

unread,
Sep 12, 2012, 3:10:50 PM9/12/12
to web...@googlegroups.com
Given the following tables:

formulaImport = db.define_table('formulaImport',
 
Field('formulaImportId', 'id', readable=False),
 
Field('fileName', length=256, required=True, label='File'),
 
Field('sweptOn', 'date', required=True, label='Swept',
 
default=datetime.datetime.today()),
 
Field('importedOn', 'date', label='Imported'),
 
Field('brillPlant', length=5, required=True, label='Plant'),
 
Field('productNumber', length=30, required=True,
 label
='Product'),
 
Field('productName', length=75, required=True,
 label
='Name'),
 format
= '%(productNumber)s')
formulaImport
._plural = 'Formula Imports'

formulaImport
.fileName.requires = IS_NOT_EMPTY()
formulaImport
.brillPlant.requires = IS_NOT_EMPTY()
formulaImport
.sweptOn.requires = IS_DATE('%m/%d/%Y')
formulaImport
.productNumber.requires = IS_NOT_EMPTY()
formulaImport
.productName.requires = IS_NOT_EMPTY()
formulaImport
.importedOn.requires = IS_NULL_OR(IS_DATE('%m/%d/%Y'))

formulaImportLine
= db.define_table('formulaImportLine',
 
Field('formulaImportLineId', 'id', readable=False),
 
Field('formulaImportId', db.formulaImport, required=True,
 label
='Formula Import'),
 
Field('sequence', 'integer', required=True),
 
Field('productNumber', length=30, required=True, label='Product Number'),
 
Field('quantity', 'decimal(13,5)', required=True),
 format
=('%(productNumber)s'))
formulaImportLine
.formulaImportId.required = IS_IN_DB(db, db.formulaImport,
 
'$(fileName)s',
 
'.choose.')
formulaImportLine
.sequence.requires = [IS_INT_IN_RANGE(0,9999999),
 IS_NOT_EMPTY
()]
formulaImportLine
.productNumber.requires = IS_NOT_EMPTY()
formulaImportLine
.quantity.requires = [IS_FLOAT_IN_RANGE(0,2000),
 IS_NOT_EMPTY
()]
db
.formulaImportLine.formulaImportId.represent = lambda p, r: r.productNumber


Given a specific formulaImportLine I want to be able to display the associate formulaLine.productNumber.

Running this code:
for x in db(db.formulaImportLine.formulaImportLineId==63901).select():
 
print x.formulaImportId.represent()

...I get the following traceback:

Traceback (most recent call last):
 
File "C:\dev\miscellaneous\playground\dalOutside.py", line 26, in <module>
 
print x.formulaImportId.represent()
TypeError: 'NoneType' object is not callable

Is there a way for me to reference a 'parent' record and display either for 'format' for the table or the 'represent' for the specific field?


Massimo Di Pierro

unread,
Sep 12, 2012, 4:02:44 PM9/12/12
to web...@googlegroups.com
Given your model you can do:

for x in db(db.formulaImportLine.formulaImportLineId==63901).select():
 
print db.formulaImportLine.formulaImportId.represent(x.formulaImportLineId)

or more simply

for x in db(db.formulaImportLine.formulaImportLineId==63901).select():
 
print x.productNumber

Jim S

unread,
Sep 12, 2012, 4:59:02 PM9/12/12
to web...@googlegroups.com
Massimo

Thanks for the reply.  Yes, I know I can do it the second way, I'm trying to a generic function working that will work with any field with the represent value set.  When I try the first suggestion I get the following:

Traceback (most recent call last):

 
File "C:\dev\miscellaneous\playground\dalOutside.py", line 28, in <module>
 
print db.formulaImportLine.formulaImportId.represent(x.formulaImportLineId)
TypeError: <lambda>() takes exactly 2 arguments (1 given)

I'm kinda lost here now...

-Jim

Massimo Di Pierro

unread,
Sep 12, 2012, 5:28:44 PM9/12/12
to web...@googlegroups.com
My bad.

print db.formulaImportLine.formulaImportId.represent(x.formulaImportLineId,x)

because you have:

... represent = lambda p, r: ...

Here p is the value of the db.formulaImportLine.formulaImportId and r is the row.
Reply all
Reply to author
Forward
0 new messages