本文整理汇总了Python中google.appengine.ext.ndb.Model方法的典型用法代码示例。如果您正苦于以下问题:Python ndb.Model方法的具体用法?Python ndb.Model怎么用?Python ndb.Model使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类google.appengine.ext.ndb
的用法示例。
在下文中一共展示了ndb.Model方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _validate_filters_ndb
# 需要导入模块: from google.appengine.ext import ndb [as 别名]
# 或者: from google.appengine.ext.ndb import Model [as 别名]
def _validate_filters_ndb(cls, filters, model_class):
"""Validate ndb.Model filters."""
if not filters:
return
properties = model_class._properties
for f in filters:
prop, _, val = f
if prop not in properties:
raise errors.BadReaderParamsError(
"Property %s is not defined for entity type %s",
prop, model_class._get_kind())
# Validate the value of each filter. We need to know filters have
# valid value to carry out splits.
try:
properties[prop]._do_validate(val)
except db.BadValueError, e:
raise errors.BadReaderParamsError(e)
示例2: __init__
# 需要导入模块: from google.appengine.ext import ndb [as 别名]
# 或者: from google.appengine.ext.ndb import Model [as 别名]
def __init__(self, model, key_name, property_name, cache=None, user=None):
"""Constructor for Storage.
Args:
model: db.Model or ndb.Model, model class
key_name: string, key name for the entity that has the credentials
property_name: string, name of the property that is a CredentialsProperty
or CredentialsNDBProperty.
cache: memcache, a write-through cache to put in front of the datastore.
If the model you are using is an NDB model, using a cache will be
redundant since the model uses an instance cache and memcache for you.
user: users.User object, optional. Can be used to grab user ID as a
key_name if no key name is specified.
"""
if key_name is None:
if user is None:
raise ValueError('StorageByKeyName called with no key name or user.')
key_name = user.user_id()
self._model = model
self._key_name = key_name
self._property_name = property_name
self._cache = cache
示例3: is_type_of
# 需要导入模块: from google.appengine.ext import ndb [as 别名]
# 或者: from google.appengine.ext.ndb import Model [as 别名]
def is_type_of(cls, root, info):
if isinstance(root, cls):
return True
if not isinstance(root, ndb.Model):
raise Exception(('Received incompatible instance "{}".').format(root))
# Returns True if `root` is a PolyModel subclass and `cls` is in the
# class hierarchy of `root` which is retrieved with `_class_key`
if (hasattr(root, '_class_key') and
hasattr(cls._meta.model, '_class_key') and
set(cls._meta.model._class_key()).issubset(
set(root._class_key()))):
return True
return type(root) == cls._meta.model
示例4: _from_base_type
# 需要导入模块: from google.appengine.ext import ndb [as 别名]
# 或者: from google.appengine.ext.ndb import Model [as 别名]
def _from_base_type(self, ent):
"""Convert a Model instance (entity) to a Message value."""
if ent._projection:
# Projection query result. Reconstitute the message from the fields.
return _projected_entity_to_message(ent, self._message_type)
blob = ent.blob_
if blob is not None:
protocol = self._protocol_impl
else:
# Perhaps it was written using a different protocol.
protocol = None
for name in _protocols_registry.names:
key = '__%s__' % name
if key in ent._values:
blob = ent._values[key]
if isinstance(blob, model._BaseValue):
blob = blob.b_val
protocol = _protocols_registry.lookup_by_name(name)
break
if blob is None or protocol is None:
return None # This will reveal the underlying dummy model.
msg = protocol.decode_message(self._message_type, blob)
return msg
示例5: _message_to_entity
# 需要导入模块: from google.appengine.ext import ndb [as 别名]
# 或者: from google.appengine.ext.ndb import Model [as 别名]
def _message_to_entity(msg, modelclass):
"""Recursive helper for _to_base_type() to convert a message to an entity.
Args:
msg: A Message instance.
modelclass: A Model subclass.
Returns:
An instance of modelclass.
"""
ent = modelclass()
for prop_name, prop in modelclass._properties.iteritems():
if prop._code_name == 'blob_': # TODO: Devise a cleaner test.
continue # That's taken care of later.
value = getattr(msg, prop_name)
if value is not None and isinstance(prop, model.StructuredProperty):
if prop._repeated:
value = [_message_to_entity(v, prop._modelclass) for v in value]
else:
value = _message_to_entity(value, prop._modelclass)
setattr(ent, prop_name, value)
return ent
示例6: testEventuallyConsistentGlobalQueryResult
# 需要导入模块: from google.appengine.ext import ndb [as 别名]
# 或者: from google.appengine.ext.ndb import Model [as 别名]
def testEventuallyConsistentGlobalQueryResult(self):
class TestModel(ndb.Model):
pass
user_key = ndb.Key('User', 'ryan')
# Put two entities
ndb.put_multi([
TestModel(parent=user_key),
TestModel(parent=user_key)
])
# Global query doesn't see the data.
self.assertEqual(0, TestModel.query().count(3))
# Ancestor query does see the data.
self.assertEqual(2, TestModel.query(ancestor=user_key).count(3))
# [END HRD_example_1]
# [START HRD_example_2]
示例7: testDeterministicOutcome
# 需要导入模块: from google.appengine.ext import ndb [as 别名]
# 或者: from google.appengine.ext.ndb import Model [as 别名]
def testDeterministicOutcome(self):
# 50% chance to apply.
self.policy.SetProbability(.5)
# Use the pseudo random sequence derived from seed=2.
self.policy.SetSeed(2)
class TestModel(ndb.Model):
pass
TestModel().put()
self.assertEqual(0, TestModel.query().count(3))
self.assertEqual(0, TestModel.query().count(3))
# Will always be applied before the third query.
self.assertEqual(1, TestModel.query().count(3))
# [END HRD_example_2]
# [START main]
示例8: query_purchase_with_customer_key
# 需要导入模块: from google.appengine.ext import ndb [as 别名]
# 或者: from google.appengine.ext.ndb import Model [as 别名]
def query_purchase_with_customer_key():
# [START purchase_with_customer_key_models]
class Customer(ndb.Model):
name = ndb.StringProperty()
class Purchase(ndb.Model):
customer = ndb.KeyProperty(kind=Customer)
price = ndb.IntegerProperty()
# [END purchase_with_customer_key_models]
def query_purchases_for_customer_via_key(customer_entity):
purchases = Purchase.query(
Purchase.customer == customer_entity.key).fetch()
return purchases
return Customer, Purchase, query_purchases_for_customer_via_key
示例9: query_purchase_with_ancestor_key
# 需要导入模块: from google.appengine.ext import ndb [as 别名]
# 或者: from google.appengine.ext.ndb import Model [as 别名]
def query_purchase_with_ancestor_key():
# [START purchase_with_ancestor_key_models]
class Customer(ndb.Model):
name = ndb.StringProperty()
class Purchase(ndb.Model):
price = ndb.IntegerProperty()
# [END purchase_with_ancestor_key_models]
def create_purchase_for_customer_with_ancestor(customer_entity):
purchase = Purchase(parent=customer_entity.key)
return purchase
def query_for_purchases_of_customer_with_ancestor(customer_entity):
purchases = Purchase.query(ancestor=customer_entity.key).fetch()
return purchases
return (Customer, Purchase,
create_purchase_for_customer_with_ancestor,
query_for_purchases_of_customer_with_ancestor)
示例10: create_entity_with_parent_keys
# 需要导入模块: from google.appengine.ext import ndb [as 别名]
# 或者: from google.appengine.ext.ndb import Model [as 别名]
def create_entity_with_parent_keys():
account_key = ndb.Key(Account, 'sandy@example.com')
# Ask Datastore to allocate an ID.
new_id = ndb.Model.allocate_ids(size=1, parent=account_key)[0]
# Datastore returns us an integer ID that we can use to create the message
# key
message_key = ndb.Key('Message', new_id, parent=account_key)
# Now we can put the message into Datastore
initial_revision = Revision(
message_text='Hello', id='1', parent=message_key)
initial_revision.put()
return initial_revision
示例11: _is_ndb
# 需要导入模块: from google.appengine.ext import ndb [as 别名]
# 或者: from google.appengine.ext.ndb import Model [as 别名]
def _is_ndb(self):
"""Determine whether the model of the instance is an NDB model.
Returns:
Boolean indicating whether or not the model is an NDB or DB model.
"""
# issubclass will fail if one of the arguments is not a class, only
# need worry about new-style classes since ndb and db models are
# new-style
if isinstance(self._model, type):
if ndb is not None and issubclass(self._model, ndb.Model):
return True
elif issubclass(self._model, db.Model):
return False
raise TypeError('Model class not an NDB or DB model: %s.' %
(self._model,))
示例12: test_store_new_version_extra
# 需要导入模块: from google.appengine.ext import ndb [as 别名]
# 或者: from google.appengine.ext.ndb import Model [as 别名]
def test_store_new_version_extra(self):
# Includes an unrelated entity in the PUT. It must be in the same entity
# group.
cls = monotonic.get_versioned_root_model('fidoula')
parent = ndb.Key(cls, 'foo')
class Unrelated(ndb.Model):
b = ndb.IntegerProperty()
unrelated = Unrelated(id='bar', parent=parent, b=42)
actual = monotonic.store_new_version(
EntityX(a=1, parent=parent), cls, extra=[unrelated])
self.assertEqual(
ndb.Key('fidoula', 'foo', 'EntityX', monotonic.HIGH_KEY_ID), actual)
actual = monotonic.store_new_version(EntityX(a=2, parent=parent), cls)
self.assertEqual(
ndb.Key('fidoula', 'foo', 'EntityX', monotonic.HIGH_KEY_ID - 1), actual)
self.assertEqual({'b': 42}, unrelated.key.get().to_dict())
示例13: shard_key
# 需要导入模块: from google.appengine.ext import ndb [as 别名]
# 或者: from google.appengine.ext.ndb import Model [as 别名]
def shard_key(key, number_of_letters, root_entity_type):
"""Returns an ndb.Key to a virtual entity of type |root_entity_type|.
This key is to be used as an entity group for database sharding. Transactions
can be done over this group. Note that this sharding root entity doesn't have
to ever exist in the database.
Arguments:
key: full key to take a subset of. It must be '[0-9a-f]+'. It is assumed
that this key is well distributed, if not, use hashed_shard_key()
instead. This means the available number of buckets is done in
increments of 4 bits, e.g. 16, 256, 4096, 65536.
number_of_letters: number of letters to use from |key|. key length must be
encoded through an out-of-band mean and be constant.
root_entity_type: root entity type. It can be either a reference to a
ndb.Model class or just a string.
"""
assert _HEX.issuperset(key), key
assert isinstance(key, str) and len(key) >= number_of_letters, repr(key)
# number_of_letters==10 means 1099511627776 shards, which is unreasonable.
assert 1 <= number_of_letters < 10, number_of_letters
assert isinstance(root_entity_type, (ndb.Model, str)) and root_entity_type, (
root_entity_type)
return ndb.Key(root_entity_type, key[:number_of_letters])
示例14: get_versioned_most_recent_with_root_async
# 需要导入模块: from google.appengine.ext import ndb [as 别名]
# 或者: from google.appengine.ext.ndb import Model [as 别名]
def get_versioned_most_recent_with_root_async(cls, root_key):
"""Returns the most recent instance of a versioned entity and the root entity.
Getting the root entity is needed to get the current index.
"""
# Using a cls.query(ancestor=root_key).get() would work too but is less
# efficient since it can't be cached by ndb's cache.
assert not ndb.in_transaction()
assert issubclass(cls, ndb.Model), cls
assert root_key is None or isinstance(root_key, ndb.Key), root_key
root = root_key.get()
if not root or not root.current:
raise ndb.Return(None, None)
entity = yield ndb.Key(cls, root.current, parent=root_key).get_async()
raise ndb.Return(root, entity)
示例15: _delete_tags
# 需要导入模块: from google.appengine.ext import ndb [as 别名]
# 或者: from google.appengine.ext.ndb import Model [as 别名]
def _delete_tags(model, tag, cursor=None, num_updated=0, batch_size=100):
"""Cleans up any entities on the given model that reference the given key.
Args:
model: ndb.Model, a Model with a repeated TagData property.
tag: Tag, an instance of a Tag model.
cursor: Optional[datastore_query.Cursor], pointing to the last result.
num_updated: int, the number of entities that were just updated.
batch_size: int, the number of entities to include in the batch.
"""
entities, next_cursor, more = model.query(
model.tags.tag == tag).fetch_page(batch_size, start_cursor=cursor)
for entity in entities:
entity.tags = [
model_tag for model_tag in entity.tags if model_tag.tag != tag
]
ndb.put_multi(entities)
num_updated += len(entities)
logging.info(
'Destroyed %d occurrence(s) of the tag with URL safe key %r',
len(entities), tag.key.urlsafe())
if more:
deferred.defer(
_delete_tags, model, tag,
cursor=next_cursor, num_updated=num_updated, batch_size=batch_size)
else:
logging.info(
'Destroyed a total of %d occurrence(s) of the tag with URL safe key %r',
num_updated, tag.key.urlsafe())