本文整理汇总了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]