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
On Tue, Jul 14, 2015 at 2:48 PM, LoveWeb2py <[email protected]> wrote:
> 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
>
>
> On Tuesday, July 14, 2015 at 2:32:36 PM UTC-4, Samuel Sowah wrote:
>>
>> 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.
>>
>> On Tuesday, July 14, 2015 at 6:07:39 PM UTC, 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)
>>>
>>> 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'}>
>>>
>> --
> 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.
>
--
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.