當前位置: 首頁>>代碼示例>>Python>>正文


Python related_descriptors.ReverseOneToOneDescriptor方法代碼示例

本文整理匯總了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) 
開發者ID:c3nav,項目名稱:c3nav,代碼行數:22,代碼來源:wrappers.py

示例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)) 
開發者ID:HealthByRo,項目名稱:drf_tweaks,代碼行數:9,代碼來源:optimizator.py

示例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] 
開發者ID:TriplePoint-Software,項目名稱:django_audit_trail,代碼行數:49,代碼來源:watcher.py


注:本文中的django.db.models.fields.related_descriptors.ReverseOneToOneDescriptor方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。