customize requires in DB

46 views
Skip to first unread message

Diego Tostes

unread,
Jun 20, 2018, 10:40:18 AM6/20/18
to web...@googlegroups.com
I have those tables:

Produto = db.define_table('produto',
    Field('ficha_tecnica', 'upload', label=T("Ficha Tecnica")),
    Field('cliente_id', 'integer', label=T("Cliente ID")),
    Field('cliente_nome', 'string', label=T("Nome Cliente"))
)

Pedido = db.define_table('pedido',
    Field('produto_id', db.produto),
    Field('ordem_de_servico', 'string', label=T("Ordem de Servico")),
    Field('tipo_de_pedido', 'integer', label=T("Tipo de Pedido"))
)

Materia_Prima = db.define_table('materia_prima',
    Field('pedido_id', 'integer', label=T("ID DO PEDIDO")),
    Field('data_recebimento', 'date', label=T("Data Recebimento")),
)

#requires
Pedido.produto_id.requires = IS_IN_DB(db(Produto), 'produto.id', '%(cliente_nome)s - referencia: %(referencia)s')
Materia_Prima.pedido_id.requires = IS_IN_DB(db(Pedido), 'pedido.id', )


I need that the field Materia_Prima.pedido_id, persist the pedido_id but when i select in the dropdow of a SQLFORM the information is:

'%(cliente_nome)s - referencia: %(ordem_de_servico)s'  

but the field cliente_nome is a filed from Produto.

is it Possible?


Rgds

Diego


Anthony

unread,
Jun 20, 2018, 10:56:13 AM6/20/18
to web2py-users
Materia_Prima.pedido_id.requires = IS_IN_DB(db(Pedido), 'pedido.id',

                                           
lambda r: '%s - referencia: %s' % (r.produto_id.cliente_nome,
                                                                               r
.ordem_de_servico))

Note, r.produto_id.cliente_nome is a recursive select (it will result in an additional database query for each item in the dropdown list).

Also, why isn't pedido_id a reference field?

Note, you can avoid manually specifying the IS_IN_DB validators if you instead make all of the relevant fields reference fields and then specify the "format" argument of the two referenced tables (i.e., the "label" arguments you are using in the IS_IN_DB validators can instead be specified as the "format" arguments in the definitions of the respective referenced tables). In that case, web2py will automatically define the IS_IN_DB validators for you.

Anthony

Diego Tostes

unread,
Jun 20, 2018, 11:05:36 AM6/20/18
to web...@googlegroups.com
Thanks Antony.

I will code again with your tips.

Rgds

Diego

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Diego Tostes

unread,
Jun 27, 2018, 11:25:23 AM6/27/18
to web...@googlegroups.com
Hi Anthony,


i think that put the requires make my code more organized ... But it is a personal choice. I did changes in my tables and now i have:


Clientes = db.define_table('clientes',
    Field('cnpj', 'string', label=T("CNPJ")),
    Field('nome_cliente', 'string', label=T("Cliente")),
    Field('email', 'string', label=T("E-mail")),
    Field('telefone', 'string', label=T("Telefone")),
    Field('responsavel_contato', 'string', label=T("Responsável")),
)

Produto = db.define_table('produto',
    Field('ficha_tecnica', 'upload', label=T("Ficha Tecnica")),
    Field('descricao_produto', 'string', label=T("Descricao Produto")),
    Field('cliente_id', db.clientes, label=T("Cliente ID")),
)

Ordem_Servico = db.define_table('ordem_servico',
    Field('identificador_os', 'string'),
    Field('produto_id', db.produto),
    Field('finalizado', "boolean"),
    )

Pedido = db.define_table('pedido',
    Field('ordem_servico_id', db.ordem_servico),
    Field('tipo_pedido', "integer", label=T("tp"))


Pedido.produto_id.requires = IS_IN_DB(Ordem_Servico), 'ordem_servico.id',
                                            lambda r: '%s - os: %s' % (r.ordem_servico_id.produto_id.cliente_id.nome_cliente,
                                                                               r.ordem_servico_id.identificador_os))

----> is it possible to "navigate" like this?  

r.ordem_servico_id.produto_id.cliente_id.nome_cliente

to get the "nome_cliente" ?

Rgds


Diego

Diego Tostes

unread,
Jun 27, 2018, 11:32:40 AM6/27/18
to web...@googlegroups.com
Pedido.ordem_servico_id.requires = IS_IN_DB(Ordem_Servico), 'ordem_servico.id',
                                            lambda r: '%s - os: %s' % (r.ordem_servico_id.produto_id.cliente_id.nome_cliente,
                                                                               r.ordem_servico_id.identificador_os))

Anthony

unread,
Jun 27, 2018, 12:01:06 PM6/27/18
to web2py-users
Pedido.produto_id.requires = IS_IN_DB(Ordem_Servico), 'ordem_servico.id',
                                            lambda r: '%s - os: %s' % (r.ordem_servico_id.produto_id.cliente_id.nome_cliente,
                                                                               r.ordem_servico_id.identificador_os))

----> is it possible to "navigate" like this?  

r.ordem_servico_id.produto_id.cliente_id.nome_cliente

to get the "nome_cliente" ?

I believe that should work. Is it not doing what you expect? Note, that will result in two additional database selects for each record in ordem_servico.

Anthony
Reply all
Reply to author
Forward
0 new messages