IS_IN_DB with 2 tables

45 views
Skip to first unread message

Guilherme Germano Silva

unread,
Jun 13, 2018, 12:03:28 PM6/13/18
to web2py-users
Hi guys,

I've this tables:
db.define_table('TipoUnidade',
                Field('TipoUnidadeDescricao'),
                format='%(TipoUnidadeDescricao)s'
                )

db.define_table('Produto',
                Field('ID_TipoUnidade', 'reference TipoUnidade', requires=IS_IN_SET(['Unidade', 'Gramas'])),
                Field('CodigoBarras', type='integer'),
                Field('CodigoCacauShow', type='integer'),
                Field('CustoUnitario', type='double'),
                Field('QuantidadeMinima', type='integer'),
                Field('ProdutoDescricao', type='string', label='Produto'),
                format='%(ProdutoDescricao)s'
                )

db.define_table('EntradaProdutoEstoque',
                Field('ID_Produto', 'reference Produto'),
                Field('Ativo', type='boolean', default=True),
                Field('Validade', type='date'),
                Field('Data', type='date' ),
                Field('Quantidade', type='integer'),
                Field('DataDesativacao',type='date'),
                Field('Lote'),
                format='%(Lote)s' + ' - ' + '%(ID_Produto)s'
                )



db.define_table('Kits',
                Field('Nome'),
                Field('ID_EntradaProduto', 'list:reference EntradaProdutoEstoque')
                #Field('ID_Produto', 'list:reference Produto')
                )

db.define_table('SaidaProdutoEstoque',
                Field('ID_EntradaProdutoEstoque', 'reference EntradaProdutoEstoque'),
                Field('CustoUnitario', type='double'),
                Field('Data', type='date'),
                Field('Quantidade', type='integer')
                )
and I've this resources:
db.EntradaProdutoEstoque.ID_Produto.requires = IS_IN_DB(db, db.Produto, db.Produto._format)
db.SaidaProdutoEstoque.ID_EntradaProdutoEstoque.requires = IS_IN_DB(db(db.EntradaProdutoEstoque.Ativo == True), db.EntradaProdutoEstoque, db.EntradaProdutoEstoque._format)

I want the table SaidaProdutoEstoque on field ID_EntradaProdutoEstoque show Lote and the product name referenced on table EntradaProdutoEstoque on Field ID_Produto where the name of product is on table Produto

It's possible? 

Anthony

unread,
Jun 13, 2018, 12:17:00 PM6/13/18
to web...@googlegroups.com
db.SaidaProdutoEstoque.ID_EntradaProdutoEstoque.requires = IS_IN_DB(db(db.EntradaProdutoEstoque.Ativo == True), db.EntradaProdutoEstoque, db.EntradaProdutoEstoque._format)

I want the table SaidaProdutoEstoque on field ID_EntradaProdutoEstoque show Lote and the product name referenced on table EntradaProdutoEstoque on Field ID_Produto where the name of product is on table Produto

It's possible?

Yes, but you cannot simply use db.EntradaProdutoEstoque._format, as that will only show the ID_Produto integer value. Instead, the third argument to IS_IN_DB can be a function:

db.SaidaProdutoEstoque.ID_EntradaProdutoEstoque.requires = IS_IN_DB(
    db
(db.EntradaProdutoEstoque.Ativo == True),
    db
.EntradaProdutoEstoque,
   
lambda r: '%s - %s' % (r.Lote, r.ID_Produto.ProdutoDescricao))

Note, r.ID_Produto.ProdutoDescricao is a recursive select -- it results in a separate select query for each item in the list. If there are many items, this may be inefficient and slow. If that becomes a problem, an alternative is to retrieve the data you need for the validator/widget via a SQL join, and then use an IS_IN_SET validator with a manually generated set of items.

Also, note that r.ID_Produto.ProdutoDescricao is equivalent to db.Produto(r.ID_Produto).ProdutoDescricao.

Anthony

Guilherme Germano Silva

unread,
Jun 13, 2018, 12:24:29 PM6/13/18
to web2py-users
Worked, thank you!
Reply all
Reply to author
Forward
0 new messages