Reputation: 62
I have a below use case:
I have a method that accepts a list of strings. For each of the strings, I need to create a property under an existing google data store entity A
Example: I have an existing entity Person
with properties fname
and lname
.
If the input list has strings - address
, city
, I need to update the entity Person
to include these new properties address
and city
.
I'm coding this use case in Python. Any suggestions on how I can achieve this?
Upvotes: 1
Views: 1077
Reputation: 2537
So the best way to do this is to let your class inherit ndb.Expando
. The difference between Expando
and Model
is that you can always add attributes to an Expando
entity and be able to store it in the Datastore.
Knowing this, there are several ways to proceed, but I am guessing you’re also going to need to use Python’s setattr(object, name, value)
method to pass the attribute name from a string.
Upvotes: 3
Reputation: 191
Take a look at the Expando model class: https://cloud.google.com/appengine/docs/standard/python/ndb/creating-entity-models#creating_an_expando_model_class
class Person(ndb.Expando):
pass
person = Person(fname='some', lname='body')
person_key = person.put()
...
person = person_key.get()
person.city = 'San Francisco'
person.address = '1234 Main St.'
person.put()
Upvotes: 2