本文整理匯總了Python中django.db.models.fields.related_descriptors.ReverseOneToOneDescriptor方法的典型用法代碼示例。如果您正苦於以下問題:Python related_descriptors.ReverseOneToOneDescriptor方法的具體用法?Python related_descriptors.ReverseOneToOneDescriptor怎麽用?Python related_descriptors.ReverseOneToOneDescriptor使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.db.models.fields.related_descriptors
的用法示例。
在下文中一共展示了related_descriptors.ReverseOneToOneDescriptor方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __getattr__
# 需要導入模塊: from django.db.models.fields import related_descriptors [as 別名]
# 或者: from django.db.models.fields.related_descriptors import ReverseOneToOneDescriptor [as 別名]
def __getattr__(self, name):
descriptor = getattr(self._obj.__class__, name, None)
if isinstance(descriptor, ReverseOneToOneDescriptor):
try:
rel_obj = descriptor.related.get_cached_value(self._obj)
except KeyError:
related_pk = self._obj._get_pk_val()
if related_pk is None:
rel_obj = None
else:
related_model = self._wrap_model(descriptor.related.related_model)
filter_args = descriptor.related.field.get_forward_related_filter(self._obj)
try:
rel_obj = related_model.objects.get(**filter_args)
except related_model.DoesNotExist:
rel_obj = None
else:
descriptor.related.field.set_cached_value(rel_obj, self._obj)
descriptor.related.set_cached_value(self._obj, rel_obj)
return super().__getattr__(name)
示例2: check_if_related_object
# 需要導入模塊: from django.db.models.fields import related_descriptors [as 別名]
# 或者: from django.db.models.fields.related_descriptors import ReverseOneToOneDescriptor [as 別名]
def check_if_related_object(model_field):
if LooseVersion(get_version()) >= LooseVersion("1.9"):
return any(isinstance(model_field, x) for x in (related_descriptors.ForwardManyToOneDescriptor,
related_descriptors.ReverseOneToOneDescriptor))
else:
return any(isinstance(model_field, x) for x in (related_descriptors.SingleRelatedObjectDescriptor,
related_descriptors.ReverseSingleRelatedObjectDescriptor))
示例3: init_related_signals
# 需要導入模塊: from django.db.models.fields import related_descriptors [as 別名]
# 或者: from django.db.models.fields.related_descriptors import ReverseOneToOneDescriptor [as 別名]
def init_related_signals(self):
if not self.track_related:
return
for attr_name in self.track_related:
attribute = getattr(self.model_class, attr_name)
from django import VERSION
related_model = None
if VERSION > (1, 9):
if hasattr(attribute, 'related_manager_cls'): # related object is queryset
related_model = attribute.field.model
related_field_name = attribute.field.name
else: # related object is FK
if isinstance(attribute, ReverseOneToOneDescriptor):
related_model = attribute.related.related_model
related_field_name = attribute.related.name
elif isinstance(attribute, ForwardManyToOneDescriptor):
related_model = attribute.field.related_model
related_field_name = attribute.field.related_query_name()
# related_query_name() returns related_name if it was set
# but if it's not returns autogenerated related name without '_set' postfix!
# F.e. instead of 'post_set' it'll return 'post' so we have to handle it manually
if not hasattr(related_model, related_field_name):
related_field_name += '_set'
else:
if hasattr(attribute, 'related'): # related object is queryset
related = attribute.related
related_model = related.related_model
related_field_name = related.field.name
else: # related object is FK
related_model = attribute.field.related_field.model
related_field_name = attribute.field.related_query_name()
# related_query_name() returns related_name if it was set
# but if it's not returns autogenerated related name without '_set' postfix!
# F.e. instead of 'post_set' it'll return 'post' so we have to handle it manually
if not hasattr(related_model, related_field_name):
related_field_name += '_set'
if not hasattr(related_model, 'audit'):
related_watcher = AuditTrailWatcher(track_only_with_related=True)
related_watcher.contribute_to_class(related_model)
related_watcher.init_signals()
related_model.audit.notify_related = related_model.audit.notify_related or []
related_model.audit.notify_related += [related_field_name]