python - sqlalchemy map object which contains dicts -
i'm trying map object looks this:
self.user = {lots of stuff in here} self.timestamp = date object self.coordinates = {lots of stuff in here} self.tweet = {lots of stuff in here} self.favourite = 0 self.retweet = 0
the non dictionaries seem simple map
__tablename__ = 'tweet' id = column(integer, primary_key=true) timestamp = column(datetime) favourite = column(integer) retweet = column(integer)
however have no idea how can map dictionary objects. idealy objects should proberly go there own tables obey 3rd normal form. have no idea begin. can point me in right direction? should turn these dictionaries there own objects , map them well?
many thanks
for storing dictionary objects, have several options:
- use text field, write dict via
json.dumps(value)
, read usingjson.loads(db_value)
create own json type, suggested in thread: sqlalchemy json blob/text
import jsonpickle import sqlalchemy.types types class jsontype(types.mutabletype, types.typedecorator): impl = types.unicode def process_bind_param(self, value, engine): return unicode(jsonpickle.encode(value)) def process_result_value(self, value, engine): if value: return jsonpickle.decode(value) else: # default can list return {}
and, fyi, pretty hard follow 3rd normal form, because tweet objects don't have strict , defined schema - storing in database field ok.
by way, i've found using mongodb storing tweets pretty convinient, because it's schemaless , stores json objects.
hope helps.
Comments
Post a Comment