One to many represent as_json

54 views
Skip to first unread message

LoveWeb2py

unread,
Jul 14, 2015, 2:07:39 PM7/14/15
to web...@googlegroups.com
Hello,

My goal is to represent the name instead of the field id here is my model:

db.define_table('vendors',
         Field('name','string'),
         Field('vendor_location','string'),
         format='%(name)s', migrate=True)

db.define_table('product',
         Field('product_code','string'),
         Field('product_name','string'),
         Field('product_price','string'),
         Field('product_owner', 'reference vendors'),
         Field('description','text'),
         Field('image','string', label='Image Path'),
         #Field('product_picture', 'upload', uploadfield='product_picture_file'),
         #Field('product_picture_file', 'blob'),
         format=lambda r: '%s' % (r.product_owner.name), migrate=True)


The problem is when I try to select the field it only shows me the id and not the name of the referenced record. I've seen a few of Anthony's post, but I can't seem to wrap my head around what's happening.

Here you'll see it returns the product_owner as 2L instead of the actual name of the owner. Is my format/representation messed up in my model?

In [1]: products = db(db.product.id>0).select()In [2]: products.first()Out[2]: <Row {'product_owner': 2L, 'description': 'This is my product description', 'image': '../static/images/image2.png', 'id': 1L, 'product_price': '13.99', 'product_code': '0001', 'product_name': 'my great product'}>

Dave S

unread,
Jul 14, 2015, 2:29:00 PM7/14/15
to web...@googlegroups.com


On Tuesday, July 14, 2015 at 11:07:39 AM UTC-7, LoveWeb2py wrote:
Hello,

My goal is to represent the name instead of the field id here is my model:

db.define_table('vendors',
         Field('name','string'),
         Field('vendor_location','string'),
         format='%(name)s', migrate=True)


I may be missing something, but it doesn't look like you have a "Field('name'')" for the format to work on.

/dps
 

Samuel Sowah

unread,
Jul 14, 2015, 2:32:36 PM7/14/15
to web...@googlegroups.com
you can do
db(db.vendors.id==products.first().product_owner).select().first().name
to get the name.

The actual value stored in the db is the id. format helps with select drop-downs and viewing db records in the appadmin. As far as I know, that's how it works but I'd be happy to learn if there are other ways to achieve what you're looking for.

LoveWeb2py

unread,
Jul 14, 2015, 2:48:55 PM7/14/15
to web...@googlegroups.com
I'm aware of the name method, Sam. The trouble I'm having is if I have multiple records how could I put them in json format. I was hoping I wouldn't have to iterate through the record and use the .name method

Richard Vézina

unread,
Jul 14, 2015, 3:11:18 PM7/14/15
to web2py-users
Why not defining a representation for your product_owner field??

Field('product_owner', 'reference vendors',
         represent=lambda id, row: db.vendors(id).name if id else T('N/A')),


???

Richard

--
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 web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

LoveWeb2py

unread,
Jul 14, 2015, 5:07:34 PM7/14/15
to web...@googlegroups.com
Hi Richard,

I tries this, but it still comes back as a long

db.define_table('vendors',
         Field('name','string'),
         Field('vendor_location','string'),
         format='%(name)s', migrate=True)

db.define_table('product',
         Field('product_code','string'),
         Field('product_name','string'),
         Field('product_price','string'),
         Field('product_owner', 'reference vendors', represent=lambda id, row: db.vendors(id).name if id else T('N/A')),
         Field('description','text'),
         Field('image','string', label='Image Path'),
         #Field('product_picture', 'upload', uploadfield='product_picture_file'),
         #Field('product_picture_file', 'blob'),
         migrate=True)

Dave S

unread,
Jul 14, 2015, 5:14:56 PM7/14/15
to web...@googlegroups.com


On Tuesday, July 14, 2015 at 11:29:00 AM UTC-7, Dave S wrote:


On Tuesday, July 14, 2015 at 11:07:39 AM UTC-7, LoveWeb2py wrote:
Hello,

My goal is to represent the name instead of the field id here is my model:

db.define_table('vendors',
         Field('name','string'),
         Field('vendor_location','string'),
         format='%(name)s', migrate=True)


I may be missing something, but it doesn't look like you have a "Field('name'')" for the format to work on.

Whoops, I did miss it.

LoveWeb2py

unread,
Jul 16, 2015, 11:17:24 PM7/16/15
to web...@googlegroups.com
Any other thoughts on how this could happen? I think I've seen it done before
Reply all
Reply to author
Forward
0 new messages