SQLalchemy and JSON w/ Pylons - Best Practices
August 17, 2009 at 04:32 PM | categories: sqlalchemy, pylons | View CommentsI asked the question I Stackoverflow and maybe it was too generic for the site, since it just got trolled with "Google keyword" by some d-bag. So I deleted it and figured I'd throw it up on my blog a see about getting some feedback from the people who read this pile about. The reason I ask this is mainly because I am preparing to do some updated screencasts for Pylons. I've seen multiple ways referenced in official docs and I have done it a few different ways myself. I am using Pylons and I am curious what the best practices are for this common scenario? I have used something similar to this for auto-magically making the conversion happen.
# The auto-magic version
# I pulled this off a blog, forget the source.
def _sa_to_dict(obj):
for item in obj.__dict__.items():
if item[0][0] is '_':
continue
if isinstance(item[1], str):
yield [item[0], item[1].decode()]
else:
yield item
def json(obj):
if isinstance(obj, list):
return dumps(map(dict, map(_sa_to_dict, obj)))
else:
return dumps(dict(_sa_to_dict(obj)))
# here is the controller
@jsonify
def index(self, format='html'):
templates = Session.query(Template).all()
if format == 'json':
return json(templates)
I have also done the version where you use the jsonify decorator and build your dictionary manually, something like this, which is ok if I need to define some custom behavior for my JSON, but as the default behavior seems excessive.
@jsonify
def index(self, format='html'):
if format == 'json':
q = Session.query
templates = [{'id': t.id,
'title': t.title,
'body': t.body} for t in q(Template)]
return templates
I've also created an inherited SA class which defines a json method and have used that on all my objects to convert them to JSON. Similar to the the fedora extensions.
Maybe I missed some obviously library out there or some obvious helper in the Pylons packages, but I feel like this is a very common task being done a dozen different ways between docs, source, and my own personal projects. Curious what others are doing / using.
jQuery getJSON and IE cache
August 13, 2009 at 05:55 PM | categories: jquery | View Comments
I am sure everyone else who lives in the world of jQuery knows this, so this is more here for my own future reference and avoiding and more wasting of time, but if you are going to us $.getJSON helper in jQuery, append a time stamp to it to prevent odd caching behavior in IE. See the example below.
$.getJSON("/story/story_id.php?_="+(new Date().getTime()), function(json) {
$('#next_story_id').html('S' + json.next_story_id);
});
This might not be the best way to do it and really, this is good practice for all dynamic requests to avoid caching issues from the browser and the server. Chalk this up as a good learning experience that sucked 30 minutes of my life. I hate front end stuff.