On Nov 7, 2006, at 10:08 PM, chiangf wrote:
>
> I have a page that requests for JSON output from controller, which
> returns a list of selectresults from an SQLObject. My table looks
> something like this:
>
> class myTable(SQLObject):
> name = UnicodeCol()
> date = DateTimeCol()
> myfk = ForeignKey('SecondTable')
>
> class secondTable(SQLObject):
> secondname = UnicodeCol()
>
> I have no problem displaying the variables 'name' and 'date' but I
> can't seem to get myfk. In python code, I'm able to access
> myfk.secondname and I want to do something similar in the javascript
> code. I understand that myfk is kind of like a pointer to the other
> table object and perhaps javascript doesn't understand this.
>
> My question is whether there is any way to access myfk.secondname from
> javascript
Yes as long as myfk get's jsonified too. By default ForeignKeys are
not jsonified because, as Jorge pointed out, you could end up pulling
too much data if every existing join was carelesly performed. The
jsonify rule could look something like:
from turbojson import jsonify
@jsonify.when("isinstance(obj, myTable)")
def jsonify_myTable(next_method, obj):
# first get the output of the next most specific rule, that is,
"jsonify_sqlobject"
output = next_method(obj)
# Now add the jsonified joined object
output['myfk'] = jsonify(obj.myfk)
return output
The resulting json should look something like:
{'name':'foo', 'date':'a_date', 'myfk':{'secondname':'bar', ....}}
so from JS you can do: data['myfk']['secondname']
Hope it helps,
Alberto
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---