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.
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.
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.
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.
You can use json
You could pickle the dictionary and store it as a StringProperty.
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.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With