class MyVirtualFields(object):

        def artist_name(self):

            artistName = db.artist(self.album.artist_id).name

            return artistName


db.album.virtualfields.append(MyVirtualFields())


This did the trick… but is this considered good practice?

2011/9/1 Christian Hoffmann <[email protected]>

> Hi Richard,
>
> thanks for your quick reply. After adding that to my db.py i still get
> something like this as JSON reply
> {"content": [{"name": "the album name", "artist_id": 1, "year": null, "id":
> 1}]}
> So still no name for artist_id in JSON and not in default.html
>
>
> I tested something like this:
>
> db.album.artist_id.represent = lambda id: db.artist(id).name
>
>
> This works in HTML (the table shows the correct name) but not in JSON, this
> gives still the artist_id as integer.
>
> Also I'd still be able to access the id. Because of this I thought of the
> extra field.
>
> Any ideas?
>
>
>
> 2011/9/1 Richard Vézina <[email protected]>
>
>> Add this below your album model...
>>
>> db.album.artist_id.requires=IS_IN_DB(db,db.artist.id,'%(name)s')
>>
>> You will have a dropbox instead of a empty field for artist_id field of
>> table album.
>>
>> Richard
>>
>> On Thu, Sep 1, 2011 at 1:19 PM, Christian <[email protected]> wrote:
>>
>>> Hello everyone,
>>> given the case, I have these 2 tables:
>>>
>>> db.define_table('artist',
>>>                Field('name'))
>>>
>>> db.define_table('album',
>>>                Field('artist_id', db.artist),
>>>                Field('name'))
>>>
>>> How would I add a field in the album table, (e.g. artist_name), that
>>> references the corrosponding db.artist.name of the artist table. I
>>> would like to access the album table via the restful api and I don't
>>> want to make 2 calls to get the album first and then the artist name
>>> via artist_id.
>>>
>>> Thanks and regards,
>>> Christian
>>>
>>
>>
>

Reply via email to