On Feb 25, 12:17 pm, Roger Demetrescu roger.demetre...@gmail.com
wrote:
Note that this implementation is very simple. Depending of your use
case, you probably should take a look at MutableType [1] and
types.TypeEngine.is_mutable().
[1] -http://www.sqlalchemy.org/docs/05/reference/sqlalchemy/types.html#sql...
On Wed, Feb 25, 2009 at 10:27 AM, Roger Demetrescu
roger.demetre...@gmail.com wrote:
I did something like that recently:
-
from sqlalchemy import types
import simplejson
class JsonString(types.TypeDecorator):
impl = types.String
def process_result_value(self, value, dialect):
if value is None:
return None
else:
return simplejson.loads(value)
def process_bind_param(self, value, dialect):
if value is None:
return None
else:
return simplejson.dumps(value)
-
This looks like something I should be using, but I'm not sure. I've
been playing around with simplejson and loads but not getting very
far when it comes to getting my json object's properties into my
python SA object's (model's) fields.
I have a simple model/class named Comment with columns id (int),
comment (text), and postdate (datetime).
Below are the main bits of the python code I'm using to test how I
should handle this:
# json from the client's browser
json = r'{id:1,postdate:Sat Jan 31 2009 22:18:15 GMT-0500
(Eastern Standard Time),comment:Comment text.}'
# create comment instance
comment = model.Comment()
# decode json to python object (dict)
json_obj = simplejson.loads(json)
# ???
So now I have json_obj as a python dict object with key/value
pairs... and I need to somehow get this into:
comment.id
comment.comment
comment.postdate
I've searched and searched and have only found one good example piece
of code that I may be able to use to at least handle the parsing/
generation of the datetime type from a javascript Date string; but,
I'm still stuck on how to iterate over the dict keys and do an
assignment to the properties in the comment object instance in a way
that's reusable for other model classes.
Thanks for your time, -e
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---