I have a ndb model
import os
class ProblemInvite(ndb.Model):
email = nab.StringProperty(required=True)
token = ndb.StringProperty(required=True, default=os.urandom(16).encode('hex'))
When I create a list of the model, the token is same:
import logging
for email in emails:
problem_invite = ProblemInvite(email=email_address)
logging.exception(problem_invite.token)
The weird thing is, the invite token for each email is same, what's wrong with it? Thanks.
There can only be one default value for a property type in the datastore at a time. From the Property Options table:
So your os.urandom(16).encode('hex')
expression is evaluated only once. I'm not 100% certain when, but I suspect it'd be at app deployment time - when the datastore models are uploaded.
To address the problem just drop the default value and explicitly specify the property value when you create the entity.
Side note: when using default values in ndb models you need to be extra careful at changing these default values as the behaviour (i.e. the data returned for the affected properties) may change for the entities already existing in the datastore at app deployment/update time.