本文整理汇总了Python中django.db.models.fields.related_descriptors.ForwardManyToOneDescriptor方法的典型用法代码示例。如果您正苦于以下问题:Python related_descriptors.ForwardManyToOneDescriptor方法的具体用法?Python related_descriptors.ForwardManyToOneDescriptor怎么用?Python related_descriptors.ForwardManyToOneDescriptor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models.fields.related_descriptors
的用法示例。
在下文中一共展示了related_descriptors.ForwardManyToOneDescriptor方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __set__
# 需要导入模块: from django.db.models.fields import related_descriptors [as 别名]
# 或者: from django.db.models.fields.related_descriptors import ForwardManyToOneDescriptor [as 别名]
def __set__(self, instance, value):
if value is not None or not self.field.nullable_fields:
super(CompositeForwardManyToOneDescriptor, self).__set__(instance, value)
else:
# we set only the asked fields to None, not all field as the default ForwardManyToOneDescriptor will
# ### taken from original ForwardManyToOneDescriptor
# Look up the previously-related object, which may still be available
# since we've not yet cleared out the related field.
# Use the cache directly, instead of the accessor; if we haven't
# populated the cache, then we don't care - we're only accessing
# the object to invalidate the accessor cache, so there's no
# need to populate the cache just to expire it again.
related = get_cached_value(instance, self, None)
# If we've got an old related object, we need to clear out its
# cache. This cache also might not exist if the related object
# hasn't been accessed yet.
if related is not None:
related_field = self.field.remote_field
set_cached_value_by_field(related, related_field, None)
# ##### only original part
for lh_field_name, none_value in self.field.nullable_fields.items():
setattr(instance, lh_field_name, none_value)
# Set the related instance cache used by __get__ to avoid a SQL query
# when accessing the attribute we just set.
set_cached_value_by_descriptor(instance, self, None)
示例2: check_if_related_object
# 需要导入模块: from django.db.models.fields import related_descriptors [as 别名]
# 或者: from django.db.models.fields.related_descriptors import ForwardManyToOneDescriptor [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: _get_relobjs
# 需要导入模块: from django.db.models.fields import related_descriptors [as 别名]
# 或者: from django.db.models.fields.related_descriptors import ForwardManyToOneDescriptor [as 别名]
def _get_relobjs(obj,field,filters=None):
"""Find related objects that match
Could be linked using a "related_name" or as <type>_set
django versions have changed this around so somewhat tricky..
"""
# then try to find objects of this type with a foreign key property using either (name) supplied or target object type
if not filters:
filters = { 'includes': {} , 'excludes' : {} }
if field.endswith(")") :
(field, relprop ) = str(field[0:-1]).split("(")
else :
relprop = None
try:
reltype = ContentType.objects.get(model=field)
except ContentType.DoesNotExist as e :
raise ValueError("Could not locate attribute or related model '{}' in element '{}'".format(field, type(obj)) )
# if no related_name set in related model then only one candidate and djanog creates X_set attribute we can use
try:
return getattr(obj, "".join((field,"_set"))).filter(**filters['includes']).exclude(**filters['excludes'])
except:
pass
# trickier then - need to look at models of the named type
claz = reltype.model_class()
for prop,val in list(claz.__dict__.items()) :
# skip related property names if set
if relprop and prop != relprop :
continue
if relprop or type(val) is ReverseSingleRelatedObjectDescriptor and val.field.related.model == type(obj) :
filters['includes'].update({prop:obj})
return claz.objects.filter(**filters['includes']).exclude(**filters['excludes'])
示例4: init_related_signals
# 需要导入模块: from django.db.models.fields import related_descriptors [as 别名]
# 或者: from django.db.models.fields.related_descriptors import ForwardManyToOneDescriptor [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]