Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I put a dictionary in the datastore?

Is there a good way to store a Python dictionary in the datastore? I want to do something like the following:

from google.appengine.ext import db

class Recipe(db.Model):
  name = db.StringProperty()
  style = db.StringProperty()
  yeast = db.StringProperty()
  hops = db.ListofDictionariesProperty()

Of course, that last line doesn't actually work. I need hops to be a list of key-value pairs, where the key is always a string and the value can be a string, int, or float, but I can't see anything in that would allow me to do that in the Property classes.

like image 482
Dan Hook Avatar asked Nov 11 '09 18:11

Dan Hook


People also ask

How do you store a dictionary in Python?

In Python, a dictionary can be created by placing a sequence of elements within curly {} braces, separated by 'comma'. Dictionary holds pairs of values, one being the Key and the other corresponding pair element being its Key:value.

How do you set up a dictionary?

To create a Dictionary, use {} curly brackets to construct the dictionary and [] square brackets to index it. Separate the key and value with colons : and with commas , between each pair. As with lists we can print out the dictionary by printing the reference to it.


6 Answers

Serializing a dict with repr is a good way to do it. You can then reconstitute it with eval, or if you don't trust the data, a "safe eval".

An advantage of repr over pickling is that the data is readable in the database, even queryable in desperate cases.

like image 129
Ned Batchelder Avatar answered Oct 03 '22 00:10

Ned Batchelder


You can use json

like image 20
John La Rooy Avatar answered Oct 03 '22 00:10

John La Rooy


You could pickle the dictionary and store it as a StringProperty.

like image 22
John Paulett Avatar answered Oct 03 '22 00:10

John Paulett


I'm pretty sure there's no way to store a Python dictionary. But why not just place what you'd like in hops as a second model?

Also, as mentioned by John you could use pickle, but (and correct me if I'm wrong) store it as a Blob value instead.

like image 36
Bartek Avatar answered Oct 02 '22 23:10

Bartek


Your options are basically to use pickle, to use a db.Expando and make each key in the dict a separate property, or to have a StringListProperty of keys and one of values and zip() them back to a dict when reading.

like image 28
Wooble Avatar answered Oct 03 '22 01:10

Wooble


You can use JsonProperty. Value is a Python object (such as a list or a dict or a string) that is serializable using Python's json module; Cloud Datastore stores the JSON serialization as a blob. Unindexed by default. Optional keyword argument: compressed.

from google.appengine.ext import ndb

class Article(ndb.Model):
    title = ndb.StringProperty(required=True)
    stars = ndb.IntegerProperty()
    tags = ndb.StringProperty(repeated=True)
    info = ndb.JsonProperty()
like image 43
Roy Yin Avatar answered Oct 02 '22 23:10

Roy Yin