I new app test, I have missed a line, the correct is:
db.define_table('produto',
Field('nome','string'),
Field('quant','integer',default=0),
Field('quant_min','integer',default=0)
)
class VirtualFields:
def prodEnt(self): return self.produto.quant_min * 2
db.produto.virtualfields.append(VirtualFields())
db.define_table('produto2',
Field('nome','string'),
Field('quant','integer',default=0),
Field('quant_min','integer',default=0)
)
More, this do no have effect.
On Oct 21, 11:37 am, Savio Sabino <[email protected]> wrote:
> Ops, sorry. Desconsider this. I used google translate because I
> develop in Portuguese.
> The correct:
> class VirtualFields:
>
> The code in portuguese:
>
> iAdm = db.Table(db, 'iAdm',
> Field('nome',notnull=True),
> Field('obs'),
> Field('ativo','boolean',default=True),
> Field('dt','datetime',default=request.now),
> Field('user',db.auth_user,default=auth.user_id)
> )
>
> iAdm.dt.writable=False
> iAdm.dt.readable=False
> iAdm.user.writable=False
> iAdm.user.readable=False
>
> db.define_table('tp_produto', iAdm,
> Field('pai','reference tp_produto'),
> )
> db.tp_produto.pai.requires = IS_EMPTY_OR(IS_IN_DB(db,
> db.tp_produto.id, '%(nome)s'))
> db.tp_produto.nome.requires = IS_NOT_IN_DB(db, db.tp_produto.nome)
>
> db.define_table('fabricante', iAdm)
> db.fabricante.nome.requires = IS_NOT_IN_DB(db, db.fabricante.nome)
>
> db.define_table('fornecedor', iAdm,
> Field('CNPJ'),
> Field('contato'),
> Field('email'),
> Field('telefone')
> )
> db.fornecedor.nome.requires = IS_NOT_IN_DB(db, db.fornecedor.nome)
>
> db.define_table('produto', iAdm,
> Field('tipo',db.tp_produto),
> Field('fabricante',db.fabricante),
> Field('quant','integer',default=0),
> Field('quant_min','integer',default=0)
> )
> class VirtualFields:
> def prodEnt(self): return self.produto.quant_min * 2
> db.produto.virtualfields.append(VirtualFields())
> db.produto.tipo.requires = IS_IN_DB(db, db.tp_produto.id, '%(nome)s')
> db.produto.fabricante.requires = IS_IN_DB(db, db.fabricante.id, '%
> (nome)s')
> db.produto.nome.requires = IS_NOT_IN_DB(db, db.produto.nome)
>
> db.produto.id.represent = lambda valor:
> A(valor,_href=URL(r=request,c='default',f='editProduto',args=valor))
>
> db.produto.nome.represent = lambda valor: A(valor,_href="#",
> _onclick="jQuery('#mdlEntrada').fadeIn(); return false")
>
> iOpr = db.Table(db, 'iOpr',
> Field('produto',db.produto),
> Field('fornecedor',db.fornecedor),
> Field('quant','integer'),
> Field('chamado'),
> Field('obs'),
> Field('ativo','boolean',default=True),
> Field('dt','datetime',default=request.now),
> Field('user',db.auth_user,default=auth.user_id)
> )
> iOpr.produto.requires = IS_IN_DB(db,db.produto.id,'%(nome)s')
> iOpr.fornecedor.requires = IS_IN_DB(db,db.fornecedor.id,'%(nome)s')
>
> iOpr.dt.writable=False
> iOpr.dt.readable=False
> iOpr.user.writable=False
> iOpr.user.readable=False
>
> db.define_table('estoque', iOpr)
>
> db.define_table('entrada', iOpr,
> Field('NF'),
> Field('dt_garantia','date')
> )
>
> db.define_table('saida', iOpr)
>
> The case to test virtual fields as made the most isolated as possible
> to ensure the error orign: I have remove table inheritance for table
> produto, and remove the represent.
>
> Correcting myself. The error does not occur in Appadmin but in a
> custom crud function:
>
> @auth.requires_login()
> def admin():
> args = request.args
> titulo = 'administração'
> if not args:
> link = UL(*[LI(A(tab,_href=URL(args=tab))) for tab in
> db.tables])
> return dict(items=link,titulo=titulo)
>
> if not args(1):
> i = 0
> else:
> i =1
>
> for tab in db.tables:
> if tab==args(i):
> tb = db[tab]
>
> if args(0)=='editar':
> form = crud.update(tb,
> args(2),next=URL(f='admin',args=args(1)))
> items = None
> titulo = 'Editar %s ' % args(i)
> else:
> form = crud.create(tb)
> rows = db().select(tb.ALL)
> items = SQLTABLE(rows,linkto='editar')
> titulo = 'Inserir %s ' % args(i)
>
> And the error:
>
> File "C:/web2py/applications/estoque/models/db.py", line 40, in
> prodEnt
> def prodEnt(self): return self.produto.quant_min * 2
> File "C:\web2py\gluon\sql.py", line 733, in __getattr__
> return dict.__getitem__(self,key)
> KeyError: 'quant_min'
>
> Exception: <type 'exceptions.KeyError'>('quant_min')
>
> Function argument list: (self=<Row {'estoque': <gluon.sql.Set object
> at 0x0531...'entrada': <gluon.sql.Set object at 0x0531B690>}>,
> key='quant_min')
>
> 728.
> 729.
> 730.
> 731.
> 732.
> 733.
>
> 734.
> 735.
> 736.
> 737.
>
> def __setitem__(self, key, value):
> dict.__setitem__(self, str(key), value)
>
> def __getattr__(self, key):
> return dict.__getitem__(self,key)
>
> def __setattr__(self, key, value):
> dict.__setitem__(self,key,value)
>
> * self: <Row {'estoque': <gluon.sql.Set object at
> 0x0531...'entrada': <gluon.sql.Set object at 0x0531B690>}>
> * dict.__getitem__: <method '__getitem__' of 'dict' objects>
> * builtindict: <type 'dict'>
> * key: 'quant_min'
>
> Creatin a new app test with the model:
>
> db.define_table('produto',
> Field('nome','string'),
> Field('quant','integer',default=0),
> Field('quant_min','integer',default=0)
> )
> class VirtualFields:
> def prodEnt(self): return self.produto.quant_min * 2
>
> db.define_table('produto2',
> Field('nome','string'),
> Field('quant','integer',default=0),
> Field('quant_min','integer',default=0)
> )
>
> and controller:
>
> def adm():
> return dict(f=crud())
>
> Using the Appadmin or adm function to access the table produto, There
> appears prodEnt field in any situation.
>
> On Oct 21, 10:13 am, mdipierro <[email protected]> wrote:
>
> > I assume this is a typo:
>
> > VirtualFields class:
>
> > should be
>
> > class VirtualFields:
>
> > What is the error? Can you show the traceback?
>
> > On Oct 21, 8:09 am, Savio Sabino <[email protected]> wrote:
>
> > > Hello.
> > > I did the following test with virtual fields:
>
> > > db.define_table('product',
> > > Field ('type', db.tp_produto)
> > > Field ('manufacturer', db.fabricante)
> > > Field ('qt', 'integer', default = 0),
> > > Field ('qt_min', 'integer', default = 0)
> > > )
> > > db.product.type.requires IS_IN_DB = (db, db.tp_product.id, '% (name)
> > > s')
> > > db.product.manufacturer.requires IS_IN_DB = (db, db.manufacturer.id,
> > > '% (name) s')
>
> > > VirtualFields class:
> > > def input(self): return self.product.qt_min * 2
> > > db.product.virtualfields.append(VirtualFields())
>
> > > The version of web2py in use is 1.87.3
>
> > > The new virtual field appears neither in the appadmin. What's missing?
>
> > > And, the line:
>
> > > db.produto.virtualfields.append(VirtualFields())
>
> > > causes error in the access tables below it.
>
>