Virtual Field and sqlform.grid

245 views
Skip to first unread message

Eduardo Cruz

unread,
Nov 20, 2013, 1:26:23 PM11/20/13
to web...@googlegroups.com
Whe I use a table that has some a virtual field sqlform.grid does not works

<type 'exceptions.AttributeError'> 'Row' object has no attribute 'unknown'


and that happens only when the virtual field is uncommented

def get_category_list(post):
    category_id_list
=  db(db.Post_in_category.post_id == post.id).select(db.Post_in_category.category_id)
    category_list
= []
   
for category_id in category_id_list:
        category_list
.append(db.Category[category_id["category_id"]])
   
return category_list

db
.Post.category_list = Field.Virtual(
   
lambda row: get_category_list(row.Post)
)



what am I doing wrong?

Tim Richardson

unread,
Nov 20, 2013, 3:26:48 PM11/20/13
to web...@googlegroups.com
What version of web2py?

Eduardo Cruz

unread,
Nov 20, 2013, 3:32:05 PM11/20/13
to web...@googlegroups.com
Web2py Version 2.7.4-stable+timestamp.2013.10.14.15.16.29



--
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 a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/tTWYuhhGr8I/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Eduardo Cruz - 829 775 4605 

Tim Richardson

unread,
Nov 20, 2013, 4:49:33 PM11/20/13
to web...@googlegroups.com
can you show how you make the grid? 

Eduardo Cruz

unread,
Nov 20, 2013, 4:53:08 PM11/20/13
to web...@googlegroups.com
def posts():
    grid
= SQLFORM.grid(db.Post)
   
return dict(grid=grid)Ingresar el código aquí...
 Sure.

Tim Richardson

unread,
Nov 20, 2013, 5:48:57 PM11/20/13
to web...@googlegroups.com
I can't see what's wrong.
Why don't you simply pass the id
(ie lambda row: get_category_list(row.Post.id)


and then use breakpoints to see if that is working as you expect

Tim Richardson

unread,
Nov 20, 2013, 5:52:56 PM11/20/13
to web...@googlegroups.com
Also, my experience with virtual fields is limited to returning non-iterable types, not lists. 

Eduardo Cruz

unread,
Nov 21, 2013, 7:29:36 AM11/21/13
to web...@googlegroups.com
I does not work, sadly.

Massimo Di Pierro

unread,
Nov 21, 2013, 3:54:47 PM11/21/13
to web...@googlegroups.com
What do you mean by "does not work"? Can you show a traceback? There should be no problem to returning lists.

Tim Richardson

unread,
Nov 21, 2013, 11:37:18 PM11/21/13
to web...@googlegroups.com


On Thursday, November 21, 2013 11:29:36 PM UTC+11, Eduardo Cruz wrote:
I does not work, sadly.

Where does it fail? 

is the id passed to the function ok? 

Eduardo Cruz

unread,
Nov 22, 2013, 3:39:26 PM11/22/13
to web...@googlegroups.com

<type 'exceptions.AttributeError'> 'Row' object has no attribute 'unknown'

Version

web2py™ Version 2.7.4-stable+timestamp.2013.10.14.15.16.29

Traceback
Traceback (most recent call last):
 
File "/home/eduardo/PycharmProjects/gblog/web2py/gluon/restricted.py", line 217, in restricted
   
exec ccode in environment
 
File "/home/eduardo/PycharmProjects/gblog/web2py/applications/gblog/controllers/admin.py", line 19, in <module>
 
File "/home/eduardo/PycharmProjects/gblog/web2py/gluon/globals.py", line 372, in <lambda>
   
self._caller = lambda f: f()
 
File "/home/eduardo/PycharmProjects/gblog/web2py/applications/gblog/controllers/admin.py", line 13, in posts
    grid
= SQLFORM.grid(db.Post)
 
File "/home/eduardo/PycharmProjects/gblog/web2py/gluon/sqlhtml.py", line 2374, in grid
    value
= row[str(field)]
 
File "/home/eduardo/PycharmProjects/gblog/web2py/gluon/dal.py", line 7062, in __getitem__
   
raise ae
AttributeError: 'Row' object has no attribute 'unknown'

it only fails with sqlgrid, Im using that field in another view and works flawlessly.

db.y


timestamp
= db.Table(db, 'timestamp',
                   
Field('created_on', 'datetime', default=request.now, writable=False, readable=False),
                   
Field('created_by', db.auth_user, default=auth.user_id, writable=False, readable=False),
                   
Field('updated_on', 'datetime', update=request.now, writable=False, readable=False),
                   
Field('updated_by', db.auth_user, update=auth.user_id, writable=False, readable=False))

db
.define_table("Post",
               
Field("title", label="Title"),
               
Field("body", "text", label="Content"),
                timestamp
               
)

db
.define_table("Comment",
               
Field("author", default="anonymous", label="Author"),
               
Field("body", label="Content"),
               
Field("post_id", "reference Post", writable=False, readable=False),
                timestamp
               
)

db
.define_table("Category",
               
Field("title", label="Title"),
               
Field("parent_id", "reference Category", default=None),
                timestamp
               
)

db
.define_table("Post_in_category",
               
Field("post_id", "reference Post"),
               
Field("category_id", "reference Category"),
                timestamp
               
)

def get_category_list(post_id):
    category_id_list
=  db(db.Post_in_category.post_id == post_id).select(db.Post_in_category.category_id)

    category_list
= []
   
for category_id in category_id_list:
        category_list
.append(db.Category[category_id["category_id"]])
   
return category_list

db
.Post.category_list = Field.Virtual(

   
lambda row: get_category_list(row.Post.id)
)
Message has been deleted

sunny

unread,
Jan 6, 2014, 2:33:04 PM1/6/14
to web...@googlegroups.com
Field.Virtual has a name attribute which has to be set:

db
.Post.category_list = Field.Virtual( 'category_list', lambda row: get_category_list(row.Post) )
Reply all
Reply to author
Forward
0 new messages