Thank you for your help - now it works perfect!

2013/2/26 Massimo Di Pierro <[email protected]>

> >>> db = DAL()
> >>> db.define_table('test',Field('t1'),Field('t2'))
> <Table test (id,t1,t2)>
> >>> db.test.c12 = Field.Virtual (lambda r: r.test.t1+r.test.t2)
> >>> db.test.insert(t1='x',t2='y')
> 1L
> >>> db.test(1).c12
> 'xy'
>
> Notice:
>
> db.test.c12 = Field.Virtual (lambda r: r.test.t1+r.test.t2)
>
> not
>
> db.test.c12 = Field.Virtual (lambda r: r.t1+r.t2)
>
> Moreover this is not valid:
>
> db(db.test.id>0).**select(db.test.c12)
>
> you cannot select a virtual field from database. You can select the fields
> required to compute the virtual fields:
>
> db(db.test.id>0).**select(db.test.t1, db.test.t2)
>
> and you get virtual field automatically:
>
> for row in db(db.test.id>0).select(db.test.t1, db.test.t2): print row.c12
>
>
>
> On Tuesday, 26 February 2013 15:44:43 UTC-6, mweissen wrote:
>
>> My try:
>>
>> db.define_table('test',
>>     Field('t1'),
>>     Field('t2'),
>>     )
>>
>> db.test.c12 = Field.Virtual (lambda r: r.t1+r.t2)
>>
>> def t():
>>     return dict(f=db(db.test.id>0).**select(
>>         db.test.c12,
>>         ),
>>         c=db.test(1).c12,
>>         )
>>
>> New version, new message:
>> Ticket ID
>>
>> 127.0.0.1.2013-02-26.22-38-37.**30eb0659-ae50-4985-b105-**f7303ce6b911
>> <class 'sqlite3.OperationalError'> near "<": syntax error Version web2py™ (2,
>> 4, 1, 'alpha.2', datetime.datetime(2013, 2, 26, 11, 40, 15))  Python Python
>> 2.7.3: D:\Python27\python.exe (prefix: D:\Python27)
>>
>>
>> 2013/2/26 Massimo Di Pierro <[email protected]>
>>
>>> I get:
>>>
>>> $ python web2py.py -S welcome
>>> Version 2.4.1-alpha.2+timestamp.2013.**02.26.11.40.15
>>> Database drivers available: SQLite(sqlite3), MySQL(pymysql),
>>> PostgreSQL(pg8000), IMAP(imaplib)
>>> WARNING:web2py:import IPython error; use default python shell
>>> Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
>>> [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on
>>> darwin
>>> Type "help", "copyright", "credits" or "license" for more information.
>>> (InteractiveConsole)
>>> >>> db.define_table('test',
>>> ...     Field('t1'),
>>> ...     Field('t2'),
>>> ...     )
>>> <Table test (id,t1,t2)>
>>> >>> db.test.c12 = Field.Virtual (lambda r: r.t1+r.t2)
>>> >>>
>>>
>>> On Tuesday, 26 February 2013 14:43:15 UTC-6, mweissen wrote:
>>>
>>>> Sorry, no - same message.
>>>> I have taken the last version from trunk one hour ago.
>>>>
>>>> Ticket ID
>>>>
>>>> 127.0.0.1.2013-02-26.21-39-35.****e1477239-8991-4217-810f-**9e320d**
>>>> af692c
>>>> <type 'exceptions.AttributeError'> 'FieldVirtual' object has no
>>>> attribute 'type' Version  web2py™ (2, 4, 1, 'alpha.2',
>>>> datetime.datetime(2013, 2, 25, 23, 19, 14))  Python Python 2.7.3:
>>>> D:\Python27\python.exe (prefix: D:\Python27)  Traceback
>>>>
>>>>
>>>> 1.
>>>> 2.
>>>> 3.
>>>> 4.
>>>> 5.
>>>> 6.
>>>> 7.
>>>> 8.
>>>> 9.
>>>> 10.
>>>> 11.
>>>> 12.
>>>> 13.
>>>> 14.
>>>> 15.
>>>> 16.
>>>> 17.
>>>> 18.
>>>> 19.
>>>> 20.
>>>>
>>>>
>>>> Traceback (most recent call last):
>>>>   File "D:\web2py\gluon\restricted.**py**", line 212, in restricted
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>     exec ccode in environment
>>>>   File "D:/web2py/applications/**calcte**st/controllers/default.**py" 
>>>> <http://127.0.0.1:8000/admin/default/edit/calctest/controllers/default.py>,
>>>>  line 88, in <module>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>   File "D:\web2py\gluon\globals.py", line 193, in <lambda>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>     self._caller = lambda f: f()
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>   File "D:/web2py/applications/**calcte**st/controllers/default.**py" 
>>>> <http://127.0.0.1:8000/admin/default/edit/calctest/controllers/default.py>,
>>>>  line 16, in t
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>     db.test.c12,
>>>>   File "D:\web2py\gluon\dal.py", line 9754, in select
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>     return adapter.select(self.query,fiel****ds,attributes)
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>   File "D:\web2py\gluon\dal.py", line 2218, in select
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>     return super(SQLiteAdapter, self).select(query, fields, attributes)
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>   File "D:\web2py\gluon\dal.py", line 1665, in select
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>     sql = self._select(query, fields, attributes)
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>   File "D:\web2py\gluon\dal.py", line 1520, in _select
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>     sql_f = ', '.join(map(geoexpand, fields))
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>   File "D:\web2py\gluon\dal.py", line 1517, in geoexpand
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>     if isinstance(field.type,str) and 
>>>> field.type.startswith('**geometr**y'):
>>>>
>>>>
>>>>
>>>>
>>>> AttributeError: 'FieldVirtual' object has no attribute 'type'
>>>>
>>>>
>>>>
>>>>
>>>> 2013/2/26 Massimo Di Pierro <[email protected]>
>>>>
>>>> This helped me a lot. I included these defaults in web2py so that you
>>>>> should need to do it yourself. Can you please check if
>>>>>
>>>>> db.test.c12 = Field.Virtual (lambda r: r.t1+r.t2)
>>>>>
>>>>> now works as expected?
>>>>>
>>>>> On Tuesday, 26 February 2013 05:28:59 UTC-6, Donatas Burba wrote:
>>>>>>
>>>>>> I have a workaround for this issue. Just a little helper function in
>>>>>> modules:
>>>>>>
>>>>>> def virtual_field(db, table_name, field_name, field_type,
>>>>>> compute=lambda row: None, label='', represent=None):
>>>>>>     db[table_name][field_name] = Field.Virtual(compute)
>>>>>>     db[table_name][field_name].**typ****e = field_type
>>>>>>     db[table_name][field_name].**lab****el = label
>>>>>>     db[table_name][field_name].**rep****resent = represent
>>>>>>     db[table_name][field_name].**for****matter = lambda value: value
>>>>>>     db[table_name][field_name].**com****ment = None
>>>>>>     db[table_name][field_name].**rea****dable = True
>>>>>>     db[table_name][field_name].**wri****table = False
>>>>>>      db[table_name][field_name].**req****uires = None
>>>>>>     db[table_name][field_name].**wid****get = None
>>>>>>     db[table_name][field_name].**nam****e = field_name
>>>>>>     db[table_name][field_name].**tab****lename = table_name
>>>>>>     db[table_name][field_name].**fil****ter_out = None
>>>>>>
>>>>>> Because I very often need such virtual fields (to show complex
>>>>>> computed result from several fields or even tables), this really helps 
>>>>>> me.
>>>>>>
>>>>>  --
>>>>>
>>>>>
>>>>  --
>>>
>>> ---
>>> 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+un...@**googlegroups.com.
>>>
>>> For more options, visit 
>>> https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>
>>> .
>>>
>>>
>>>
>>
>>  --
>
> ---
> 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 [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

-- 

--- 
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to