我们从Python开源项目中,提取了以下30个代码示例,用于说明如何使用google.appengine.ext.ndb.KeyProperty()。
def from_json(self, request_data): """ Update entity with new data from json. check for data to transform, if needed, perform operations and assign values to respective properties in entity. :param request_data: :return: """ for property, value in request_data.iteritems(): prop_type = self._properties.get(property) if prop_type: prop_value = value if isinstance(prop_type, (ndb.DateProperty, ndb.DateTimeProperty, ndb.TimeProperty)): prop_value = utils.date_from_str(prop_type, prop_value) elif isinstance(prop_type, ndb.KeyProperty): prop_value = ndb.Key(urlsafe=prop_value) setattr(self, property, prop_value)
def convert_KeyProperty(self, model, prop, kwargs): """Returns a form field for a ``ndb.KeyProperty``.""" if 'reference_class' not in kwargs\ or 'query' not in kwargs: try: reference_class = prop._kind except AttributeError: reference_class = prop._reference_class if isinstance(reference_class, string_types): # This assumes that the referenced module is already imported. try: reference_class = model._kind_map[reference_class] except KeyError: # If it's not imported, just bail, as we can't # edit this field safely. return None kwargs['reference_class'] = reference_class kwargs.setdefault('allow_blank', not prop._required) if prop._repeated: return RepeatedKeyPropertyField(**kwargs) else: return KeyPropertyField(**kwargs)
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
def convert_KeyProperty(self, model, prop, kwargs): """Returns a form field for a ``ndb.KeyProperty``.""" if 'reference_class' not in kwargs: try: reference_class = prop._kind except AttributeError: reference_class = prop._reference_class if isinstance(reference_class, string_types): # reference class is a string, try to retrieve the model object. mod = __import__(model.__module__, None, None, [reference_class], 0) reference_class = getattr(mod, reference_class) kwargs['reference_class'] = reference_class kwargs.setdefault('allow_blank', not prop._required) return KeyPropertyField(**kwargs)
def convert_ndb_key_propety(ndb_key_prop, registry=None): """ Two conventions for handling KeyProperties: #1. Given: store_key = ndb.KeyProperty(...) Result is 2 fields: store_id = graphene.String() -> resolves to store_key.urlsafe() store = NdbKeyField() -> resolves to entity #2. Given: store = ndb.KeyProperty(...) Result is 2 fields: store_id = graphene.String() -> resolves to store_key.urlsafe() store = NdbKeyField() -> resolves to entity """ is_repeated = ndb_key_prop._repeated name = ndb_key_prop._code_name if name.endswith('_key') or name.endswith('_keys'): # Case #1 - name is of form 'store_key' or 'store_keys' string_prop_name = rreplace(name, '_key', '_id', 1) resolved_prop_name = name[:-4] if name.endswith('_key') else p.plural(name[:-5]) else: # Case #2 - name is of form 'store' singular_name = p.singular_noun(name) if p.singular_noun(name) else name string_prop_name = singular_name + '_ids' if is_repeated else singular_name + '_id' resolved_prop_name = name return [ ConversionResult(name=string_prop_name, field=DynamicNdbKeyStringField(ndb_key_prop, registry=registry)), ConversionResult(name=resolved_prop_name, field=DynamicNdbKeyReferenceField(ndb_key_prop, registry=registry)) ]
def testKeyProperty_withSuffix(self): my_registry = Registry() class User(ndb.Model): name = ndb.StringProperty() class UserType(NdbObjectType): class Meta: model = User registry = my_registry prop = ndb.KeyProperty(kind='User') prop._code_name = 'user_key' conversion = convert_ndb_property(prop, my_registry) self.assertLength(conversion, 2) self.assertEqual(conversion[0].name, 'user_id') self.assertIsInstance(conversion[0].field, DynamicNdbKeyStringField) _type = conversion[0].field.get_type() self.assertIsInstance(_type, NdbKeyStringField) self.assertEqual(_type._type, String) self.assertEqual(conversion[1].name, 'user') self.assertIsInstance(conversion[1].field, DynamicNdbKeyReferenceField) _type = conversion[1].field.get_type() self.assertIsInstance(_type, NdbKeyReferenceField) self.assertEqual(_type._type, UserType)
def testKeyProperty_withSuffix_repeated(self): my_registry = Registry() class User(ndb.Model): name = ndb.StringProperty() class UserType(NdbObjectType): class Meta: model = User registry = my_registry prop = ndb.KeyProperty(kind='User', repeated=True) prop._code_name = 'user_keys' conversion = convert_ndb_property(prop, my_registry) self.assertLength(conversion, 2) self.assertEqual(conversion[0].name, 'user_ids') self.assertIsInstance(conversion[0].field, DynamicNdbKeyStringField) _type = conversion[0].field.get_type() self.assertIsInstance(_type, NdbKeyStringField) self.assertIsInstance(_type._type, List) self.assertEqual(_type._type.of_type, String) self.assertEqual(conversion[1].name, 'users') self.assertIsInstance(conversion[1].field, DynamicNdbKeyReferenceField) _type = conversion[1].field.get_type() self.assertIsInstance(_type, NdbKeyReferenceField) self.assertIsInstance(_type._type, List) self.assertEqual(_type._type.of_type, UserType)
def testKeyProperty_withSuffix_required(self): class User(ndb.Model): name = ndb.StringProperty() my_registry = Registry() class UserType(NdbObjectType): class Meta: model = User registry = my_registry prop = ndb.KeyProperty(kind='User', required=True) prop._code_name = 'user_key' conversion = convert_ndb_property(prop, my_registry) self.assertLength(conversion, 2) self.assertEqual(conversion[0].name, 'user_id') self.assertIsInstance(conversion[0].field, DynamicNdbKeyStringField) _type = conversion[0].field.get_type() self.assertIsInstance(_type, NdbKeyStringField) self.assertIsInstance(_type._type, NonNull) self.assertEqual(_type._type.of_type, String) self.assertEqual(conversion[1].name, 'user') self.assertIsInstance(conversion[1].field, DynamicNdbKeyReferenceField) _type = conversion[1].field.get_type() self.assertIsInstance(_type, NdbKeyReferenceField) self.assertIsInstance(_type._type, NonNull) self.assertEqual(_type._type.of_type, UserType)
def testKeyProperty_withoutSuffix(self): my_registry = Registry() class User(ndb.Model): name = ndb.StringProperty() class UserType(NdbObjectType): class Meta: model = User registry = my_registry prop = ndb.KeyProperty(kind='User') prop._code_name = 'user' conversion = convert_ndb_property(prop, my_registry) self.assertLength(conversion, 2) self.assertEqual(conversion[0].name, 'user_id') self.assertIsInstance(conversion[0].field, DynamicNdbKeyStringField) _type = conversion[0].field.get_type() self.assertIsInstance(_type, NdbKeyStringField) self.assertEqual(_type._type, String) self.assertEqual(conversion[1].name, 'user') self.assertIsInstance(conversion[1].field, DynamicNdbKeyReferenceField) _type = conversion[1].field.get_type() self.assertIsInstance(_type, NdbKeyReferenceField) self.assertEqual(_type._type, UserType)
def _get_keys_and_properties(cls): keys = {} for name, property_type in cls._properties.items(): if isinstance(property_type, ndb.KeyProperty): keys[name] = property_type._kind, property_type._repeated return keys
def __from_type_to_raw_value(cls, field, value): types = {'StringProperty': cls.__decode_str, 'IntegerProperty': cls.__to_int, 'FloatProperty': cls.__to_float, 'DateTimeProperty': cls._convert_date, 'BooleanProperty': bool, 'BlobKeyProperty': cls.__blob_to_url, 'TextProperty': cls.__decode_str, 'KeyProperty': cls.__key_property_to_id} field_type = type(field).__name__ return types[field_type](value) if field_type in types else None