def get_related_node_property_for_field(cls, field, meta_node=False):
"""
Get the relationship definition for the related node based on field.
:param field: Field to inspect
:param meta_node: If True, return the meta node for the related model,
else return the model node.
:returns: A ``RelationshipDefinition`` instance.
"""
from chemtrails.neoutils import (
__meta_cache__, __node_cache__,
get_node_class_for_model, get_meta_node_class_for_model
)
reverse_field = True if isinstance(field, (
models.ManyToManyRel, models.ManyToOneRel, models.OneToOneRel, GenericRelation)) else False
class DynamicRelation(StructuredRel):
type = StringProperty(default=field.__class__.__name__)
is_meta = BooleanProperty(default=meta_node)
remote_field = StringProperty(default=cls._get_remote_field_name(field))
target_field = StringProperty(default=str(getattr(field, 'target_field', '')).lower()) # NOTE: Workaround for #27
prop = cls.get_property_class_for_field(field.__class__)
relationship_type = cls.get_relationship_type(field)
related_model = field.model if isinstance(field, GenericForeignKey) else field.related_model
if meta_node:
klass = (__meta_cache__[related_model]
if related_model in __meta_cache__
else get_meta_node_class_for_model(related_model))
return (prop(cls_name=klass, rel_type=relationship_type, model=DynamicRelation)
# FIXME: Support cardinality for MetaNodes
if not klass._is_ignored else None)
else:
klass = (__node_cache__[related_model]
if reverse_field and related_model in __node_cache__
else get_node_class_for_model(related_model))
return (prop(cls_name=klass, rel_type=relationship_type, model=DynamicRelation,
cardinality=cls.get_cardinality_for_field(field)) if not klass._is_ignored else None)
评论列表
文章目录