Sorry, I forgot about that -- you can in fact use a virtual field. However,
for it to work properly, you must specify the table name when defining the
virtual field:
db.auth_user.full_name = Field.Virtual('full_name',
lambda row: '%s %s' %(row.auth_user.
first_name,
row.auth_user.
last_name),
table_name='auth_user')
Also, note that when you use a virtual field with autocomplete, it selects
all records (and all fields) in the table and does the filtering in Python,
so this could be quite slow for large tables.
Anthony
On Tuesday, June 6, 2017 at 9:51:33 AM UTC-4, Carlos Kitu wrote:
>
> Thanks a lot, Anthony.
> I use to check the framework code before posting questions, and when I saw
> this in the callback method of the autocomplete widget:
> def callback(self):
> if self.keyword in self.request.vars:
> field = self.fields[0]
> if type(field) is Field.Virtual:
> records = []
> table_rows = self.db(self.db[field.tablename]).select(orderby=
> self.orderby)
>
> I guessed wrongly that virtual fields were supported.
> Thank you and best regards.
>
> El lunes, 5 de junio de 2017, 17:11:37 (UTC+2), Anthony escribió:
>>
>> You cannot use autocomplete to search a virtual field. It runs a database
>> query, so the field must exist in the database.
>>
>> Anthony
>>
>> On Monday, June 5, 2017 at 9:23:51 AM UTC-4, Carlos Kitu wrote:
>>>
>>> Good afternoon,
>>> I'm trying to create an autocomplete field to search in the table
>>> db.auth_user, but the search must be done with the full name.
>>> As db.auth_user has two separate fields (first_name, and last_name), I
>>> created a virtual field to compose both fields:
>>>
>>> db.auth_user.full_name = Field.Virtual('full_name', lambda row: '%s %s'
>>> %(row.auth_user.first_name, row.auth_user.last_name))
>>>
>>> Then, I used the autocomplete widget in a field:
>>>
>>> db.define_table('my_table',
>>> Field('my_user', 'reference auth_user', notnull=True, unique=False,
>>> label='User'),
>>> ...
>>> )
>>>
>>>
>>> db.my_table.my_user.widget = SQLFORM.widgets.autocomplete(
>>> request, db.auth_user.full_name, id_field=db.auth_user.id, db=db
>>> ,
>>> at_beginning=False, limitby=(0, 10), min_length=2)
>>>
>>>
>>>
>>> And I get this error when creating a new form:
>>>
>>> <type 'exceptions.AttributeError'> 'DAL' object has no attribute 'None'
>>> Versión
>>> web2py™ Version 2.14.6-stable+timestamp.2016.05.10.00.21.47Rastreo
>>>
>>> 1.
>>> 2.
>>> 3.
>>> 4.
>>> 5.
>>> 6.
>>> 7.
>>> 8.
>>> 9.
>>> 10.
>>> 11.
>>> 12.
>>> 13.
>>> 14.
>>> 15.
>>> 16.
>>> 17.
>>> 18.
>>> 19.
>>> 20.
>>> 21.
>>> 22.
>>>
>>> Traceback (most recent call last):
>>> File
>>> "/home/clm/Documentos/p/productos/web2py_2.14.6/gluon/restricted.py", line
>>> 227, in restricted
>>> exec ccode in environment
>>> File
>>> "/home/clm/Documentos/p/productos/web2py_2.14.6/applications/VREscalas/controllers/default.py"
>>>
>>> <https://127.0.1.1:8000/admin/default/edit/VREscalas/controllers/default.py>,
>>> line 1119, in <module>
>>> File "/home/clm/Documentos/p/productos/web2py_2.14.6/gluon/globals.py",
>>> line 417, in <lambda>
>>> self._caller = lambda f: f()
>>> File "/home/clm/Documentos/p/productos/web2py_2.14.6/gluon/tools.py",
>>> line 4241, in f
>>> return action(*a, **b)
>>> File
>>> "/home/clm/Documentos/p/productos/web2py_2.14.6/applications/VREscalas/controllers/default.py"
>>>
>>> <https://127.0.1.1:8000/admin/default/edit/VREscalas/controllers/default.py>,
>>> line 140, in programas
>>> lambda row: A('resumen', _href=URL(
>>> File "/home/clm/Documentos/p/productos/web2py_2.14.6/gluon/sqlhtml.py",
>>> line 2307, in grid
>>> create_form = SQLFORM(table, **sqlformargs)
>>> File "/home/clm/Documentos/p/productos/web2py_2.14.6/gluon/sqlhtml.py",
>>> line 1288, in __init__
>>> inp = field.widget(field, default)
>>> File "/home/clm/Documentos/p/productos/web2py_2.14.6/gluon/sqlhtml.py",
>>> line 748, in __call__
>>> table_rows =
>>> self.db(self.db[self.fields[0].tablename]).select(orderby=self.orderby)
>>> File
>>> "/home/clm/Documentos/p/productos/web2py_2.14.6/gluon/packages/dal/pydal/base.py",
>>> line 914, in __getitem__
>>> return self.__getattr__(str(key))
>>> File
>>> "/home/clm/Documentos/p/productos/web2py_2.14.6/gluon/packages/dal/pydal/base.py",
>>> line 921, in __getattr__
>>> return BasicStorage.__getattribute__(self, key)
>>> AttributeError: 'DAL' object has no attribute 'None':
>>>
>>>
>>> I printed in console the self.fields[0]:
>>> self.fields {'comment': None, 'represent': <function <lambda> at
>>> 0x7f6c907310c8>, 'widget': None, 'name': 'full_name', 'f': <function
>>> <lambda> at 0x7f6c90731050>, 'requires': None, 'readable': True, 'label'
>>> : 'Full name', 'writable': False, 'filter_out': None, 'tablename': None,
>>> 'formatter': <function IDENTITY at 0x7f6c9e090140>, 'type': 'string'}
>>>
>>> It happens that the db.auth_user.full_name.tablename is None, i.e., the
>>> virtual field have no tablename attibute as the autocomplete code seems to
>>> expect.
>>> Should I open a ticket?
>>>
>>> Thank you and best regards.
>>>
>>
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.