我们从Python开源项目中,提取了以下29个代码示例,用于说明如何使用google.appengine.ext.db.ReferenceProperty()。
def getCustomProperties(self): self.reference_properties = {} self.properties = {} reverse_props = [] for name, prop in self.klass.properties().iteritems(): self.properties[name] = prop if isinstance(prop, db.ReferenceProperty): self.reference_properties[name] = prop if issubclass(self.klass, polymodel.PolyModel): del self.properties['_class'] # check if the property is a defined as a collection_name. These types # of properties are read-only and the datastore freaks out if you # attempt to meddle with it. We delete the attribute entirely .. for name, value in self.klass.__dict__.iteritems(): if isinstance(value, db._ReverseReferenceProperty): reverse_props.append(name) self.encodable_properties.update(self.properties.keys()) self.decodable_properties.update(self.properties.keys()) self.readonly_attrs.update(reverse_props) if not self.reference_properties: self.reference_properties = None if not self.properties: self.properties = None
def convert_ReferenceProperty(model, prop, kwargs): """Returns a form field for a ``db.ReferenceProperty``.""" kwargs['reference_class'] = prop.reference_class kwargs.setdefault('allow_blank', not prop.required) return ReferencePropertyField(**kwargs)
def __set__(self, instance, value): if value and not isinstance(value, db.Model): raise ValueError('You must supply a Model instance.') if not value: key = None elif self.use_key_name: key = value.key().name() else: key = str(value.key()) setattr(instance, '_ref_cache_for_' + self.target_name, value) setattr(instance, self.target_name, key) for destination, source in self.integrate.items(): integrate_value = None if value: try: property = getattr(value.__class__, source) except: property = None if property and isinstance(property, db.ReferenceProperty): integrate_value = property.get_value_for_datastore(value) else: integrate_value = getattr_by_path(value, source) setattr(instance, destination, integrate_value) # Don't use this, yet. It's not part of the official API!
def __init__(self, reference_class, integrate={}, **kwargs): self.integrate = integrate super(ReferenceProperty, self).__init__(reference_class, **kwargs)
def is_resolved(cls, property, instance): try: if not hasattr(instance, property.__id_attr_name()) or \ not getattr(instance, property.__id_attr_name()): return True return bool(getattr(instance, property.__resolved_attr_name())) except: import logging logging.exception('ReferenceProperty implementation changed! ' 'Update ragendja.dbutils.ReferenceProperty.' 'is_resolved! Exception was:') return False
def __set__(self, instance, value): super(ReferenceProperty, self).__set__(instance, value) for destination, source in self.integrate.items(): integrate_value = None if value: try: property = getattr(value.__class__, source) except: property = None if property and isinstance(property, db.ReferenceProperty): integrate_value = property.get_value_for_datastore(value) else: integrate_value = getattr_by_path(value, source) setattr(instance, destination, integrate_value)
def get_form_field(self, **kwargs): """Return a Django form field appropriate for a reference property. This defaults to a ModelChoiceField instance. """ defaults = {'form_class': ModelChoiceField, 'reference_class': self.reference_class} defaults.update(kwargs) return super(ReferenceProperty, self).get_form_field(**defaults)
def get_value_for_form(self, instance): """Extract the property value from the instance for use in a form. This return the key object for the referenced object, or None. """ value = super(ReferenceProperty, self).get_value_for_form(instance) if value is not None: value = value.key() return value
def __init__(self, property, model, use_key_name=True, integrate={}): if isinstance(property, basestring): self.target_name = property else: # Monkey-patch the target property, so we can monkey-patch the # model class, so we can detect when the user wants to set our # KeyReferenceProperty via the model constructor. # What an ugly hack; but this is the simplest implementation. :( # One alternative would be to implement a proxy model that # provides direct access to the key, but this won't work with # isinstance(). Maybe that's an option for Python 3000. # Yet another alternative would be to force the user to choose # either .key_name or .reference manually. That's rather ugly, too. self.target_name = None myself = self old_config = property.__property_config__ def __property_config__(model_class, property_name): myself.target_name = property_name my_name = None for key, value in model_class.__dict__.items(): if value is myself: my_name = key break old_init = model_class.__init__ def __init__(self, *args, **kwargs): if my_name in kwargs: setattr(self, my_name, kwargs[my_name]) kwargs[property_name] = getattr(self, property_name) for destination, source in myself.integrate.items(): integrate_value = None if kwargs[my_name]: try: property = getattr(self.__class__, source) except: property = None if property and isinstance(property, db.ReferenceProperty): integrate_value = property.get_value_for_datastore(self) else: integrate_value = getattr_by_path( kwargs[my_name], source) kwargs[destination] = integrate_value old_init(self, *args, **kwargs) model_class.__init__ = __init__ old_config(model_class, property_name) property.__property_config__ = __property_config__ self.target_model = model self.use_key_name = use_key_name self.integrate = integrate