I looked into the ndb
GitHub sample code, but I couldn't find any example
which shows on how to create a ndb
entity with a constructor that contains a StructuredProperty
.
Here is the GitHub example.
What if I want to initialize a Contact
entity with a list of phone numbers and this list of phone number is not a list of PhoneNumber
objects. Instead it is a list of Python dictionaries.
So, given the following Model
classes:
class PhoneNumber(ndb.Model):
"""A model representing a phone number."""
phone_type = ndb.StringProperty(
choices=('home', 'work', 'fax', 'mobile', 'other'))
number = ndb.StringProperty()
class Contact(ndb.Model):
"""A Contact model that uses StructuredProperty for phone numbers."""
# Basic info.
name = ndb.StringProperty()
birth_day = ndb.DateProperty()
# Address info.
address = ndb.StringProperty()
phone_numbers = ndb.StructuredProperty(PhoneNumber, repeated=True)
I want to create a Contact
using the following Python dictionaries:
phone_number_dicts = [{"phone_type" : "home", number = 122}, {"phone_type" : "work", number = 123}]
contact = Contact(name = "some name", birthday = "some day", phone_numbers = phone_number_dicts)
- Am I required to convert a dict to a
ndb
entity explicitly? - Can I override
ndb
constructor which converts a dict to andb
entity and assign? - Any other better approach?