Yes. Looks like a bug.
My previous information wasn't completly correct, so I try better:
It looks like there are 2 calls of represent= function in smartgrid. One
call requires 2 parameters and second call 1 parameter.
There is a possible solution:
Function can be called with 2nd parameter optional:
Field('parent_id', db.parent,
requires=IS_IN_DB(db, db.parent.id),
represent=lambda id, r=None: db.parent._format % db.parent(id) if id else
'')
Then all smartgrids work properly.
So fixing would be great, but not immediately neccessary.
Dne pátek, 14. února 2014 15:44:18 UTC+1 Anthony napsal(a):
>
> Looks like a bug in smartgrid -- I'll submit a patch.
>
> Anthony
>
> On Friday, February 14, 2014 5:32:35 AM UTC-5, Mirek Zvolský wrote:
>>
>> Great. Thank you.
>> (of course it would be much easier, if use of explicit requires= will
>> cause nothing with implicit represent=)
>>
>> But in one detail it must be different as your version:
>>
>> If I try
>>
>> Field('parent_id', db.parent,
>> requires=IS_IN_DB(db, db.parent.id),
>> represent=lambda id, r: db.parent._format % db.parent(id) if id else '')
>>
>> this works well in appadmin,
>> however it causes internal error in smartgrid:
>> File
>> "C:\Python27\Lib\site-packages\web2py_2_8_2\web2py\gluon\sqlhtml.py", line
>> 2764, in smartgrid
>> user_signature=user_signature, **kwargs)
>> File
>> "C:\Python27\Lib\site-packages\web2py_2_8_2\web2py\gluon\sqlhtml.py", line
>> 2450, in grid
>> value = field.represent(value, row)
>> File
>> "C:\Python27\Lib\site-packages\web2py_2_8_2\web2py\gluon\sqlhtml.py", line
>> 2708, in <lambda>
>> field.represent = lambda id, r=None, referee=referee,
>> rep=field.represent: A(callable(rep) and rep(id) or id,
>> cid=request.cid, _href=url(args=['view', referee, id]))
>> TypeError: <lambda>() takes exactly 2 arguments (1 given)
>>
>> This looks like proper solution:
>>
>> Field('parent_id', db.parent,
>> requires=IS_IN_DB(db, db.parent.id),
>> represent=lambda id: db.parent._format % db.parent(id) if id else '')
>>
>> So - your solution, but lambda with 1 parameter only.
>>
>> Thanks, Mirek
>>
>>
>>
>> Dne pátek, 14. února 2014 0:26:45 UTC+1 Anthony napsal(a):
>>>
>>> If you don't explicitly specify a validator for a reference field and
>>> the referenced table has a "format" attribute, then you get a default
>>> validator *and* a default "represent" attribute (the "represent"
>>> attribute is what is used for read-only displays). However, if you
>>> explicitly specify your own validator, then you no longer get the default
>>> "represent" attribute either, and so you must explicitly provider your own.
>>> You could do something like:
>>>
>>> Field('myfield', 'represent othertable', requires=...,
>>> represent=lambda id, r: db.othertable._format % db.othertable(id)
>>> if id else '')
>>>
>>> Anthony
>>>
>>> On Thursday, February 13, 2014 6:08:55 PM UTC-5, Mirek Zvolský wrote:
>>>>
>>>> As user of my application I don't want see any ID's at all - instead I
>>>> want always see record content formatted from format=..
>>>>
>>>> In smartgrid I am able to receive records instead of ID's only, if I
>>>> run with default validator for foreign key (i.e. without requires=..)
>>>> In such case (without requires=..) I see related records for readonly
>>>> foreign keys (View) and for readwrite foreign keys (Edit).
>>>> If I use requires=.. then for readonly foreign keys I see ID's and I
>>>> have no idea, how to prevent it.
>>>>
>>>> If I need a foreign key, which should be None for some records, then I
>>>> think it is necessary to use requires=IS_EMPTY_OR(IS_IN_DB(db,
>>>> db.parent.id))
>>>> However this will display ID's instead of formatted records for
>>>> readonly foreign keys. :(((
>>>>
>>>> How to do it proper? Can represent=.. help? Or....?
>>>>
>>>> I try it with 2.8.2 version. Thanks for help !
>>>> (Note: I don't know, if answer to my question was same in older
>>>> versions, or if it has changed in 2,8.2 with new constraint handling for
>>>> SQLite.)
>>>>
>>>>
--
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/groups/opt_out.