我们从Python开源项目中,提取了以下24个代码示例,用于说明如何使用django.contrib.contenttypes.fields.GenericRelation()。
def __init__(self, field, children=None): expressions = [] for cfield, kids in (children or {}).items(): expressions.append(IncludeExpression(cfield, kids)) if isinstance(field, GenericRelation): self._constructor = GenericRelationConstructor(field, expressions) elif getattr(field, 'many_to_many', False): self._constructor = ManyToManyConstructor(field, expressions) elif getattr(field, 'multiple', False): self._constructor = ManyToOneConstructor(field, expressions) else: self._constructor = IncludeExpressionConstructor(field, expressions) super(IncludeExpression, self).__init__(output_field=JSONField())
def _register_model(admin, model): if not hasattr(admin, 'reversion_format'): admin.reversion_format = 'json' if not is_registered(model): inline_fields = [] for inline in getattr(admin, 'inlines', []): inline_model = inline.model if getattr(inline, 'generic_inline', False): ct_field = getattr(inline, 'ct_field', 'content_type') ct_fk_field = getattr(inline, 'ct_fk_field', 'object_id') for field in model._meta.many_to_many: if isinstance(field, GenericRelation) \ and field.rel.to == inline_model \ and field.object_id_field_name == ct_fk_field \ and field.content_type_field_name == ct_field: inline_fields.append(field.name) _autoregister(admin, inline_model) else: fk_name = getattr(inline, 'fk_name', None) if not fk_name: for field in inline_model._meta.fields: if isinstance(field, (models.ForeignKey, models.OneToOneField)) and issubclass(model, field.rel.to): fk_name = field.name _autoregister(admin, inline_model, follow=[fk_name]) if not inline_model._meta.get_field(fk_name).rel.is_hidden(): accessor = inline_model._meta.get_field(fk_name).remote_field.get_accessor_name() inline_fields.append(accessor) _autoregister(admin, model, inline_fields)
def load_field(self, fieldname, val, zf, version): if val is None: return val, False try: field = self.model._meta.get_field(fieldname) except models.fields.FieldDoesNotExist: field = None deferr = False if field: if isinstance(field, models.DateTimeField): val = timezone.make_aware( datetime.datetime.strptime(val, DATETIME_FORMAT), timezone.utc) elif isinstance(field, models.DateField): val = datetime.date.strptime(val, DATE_FORMAT) elif isinstance(field, models.ManyToManyField): val = self.load_many(field.related_model, val, zf, version) deferr = True elif isinstance(field, models.ManyToOneRel): val = self.load_many(field.related_model, val, zf, version, commit=False) deferr = True elif isinstance(field, models.ForeignKey): val = load_model_instance(field.rel.to, val, zf, version) elif isinstance(field, GenericRelation): val = self.load_many(field.rel.to, val, zf, version, commit=False) deferr = True elif isinstance(val, list): rel_model = getattr(self.model, fieldname).related.related_model val = self.load_many(rel_model, val, zf, version, commit=False) deferr = True return val, deferr
def _get_remote_field_name(field): reverse_field = True if isinstance(field, ( models.ManyToManyRel, models.ManyToOneRel, models.OneToOneRel, GenericRelation)) else False return str('{model}.{field}'.format( model=get_model_string(field.model), field=(field.related_name or '%s_set' % field.name if not isinstance(field, (models.OneToOneRel, GenericRelation)) else field.name)) if reverse_field else field.remote_field.field if not isinstance(field, GenericForeignKey) else str(field)).lower()
def get_cardinality_for_field(field): """ Returns the cardinality for ``field``. """ cardinalities = { models.ForeignKey: ZeroOrOne, models.ManyToOneRel: ZeroOrMore, models.OneToOneField: ZeroOrOne, models.OneToOneRel: ZeroOrOne, models.ManyToManyField: ZeroOrMore, models.ManyToManyRel: ZeroOrMore, GenericForeignKey: ZeroOrOne, GenericRelation: ZeroOrMore } return cardinalities[field.__class__]
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)
def _register_model(admin, model): if not hasattr(admin, 'revision_manager'): admin.revision_manager = default_revision_manager if not hasattr(admin, 'reversion_format'): admin.reversion_format = 'json' if not admin.revision_manager.is_registered(model): inline_fields = [] for inline in getattr(admin, 'inlines', []): inline_model = inline.model if getattr(inline, 'generic_inline', False): ct_field = getattr(inline, 'ct_field', 'content_type') ct_fk_field = getattr(inline, 'ct_fk_field', 'object_id') for field in model._meta.many_to_many: if isinstance(field, GenericRelation) \ and field.rel.to == inline_model \ and field.object_id_field_name == ct_fk_field \ and field.content_type_field_name == ct_field: inline_fields.append(field.name) _autoregister(admin, inline_model) else: fk_name = getattr(inline, 'fk_name', None) if not fk_name: for field in inline_model._meta.fields: if isinstance(field, (models.ForeignKey, models.OneToOneField)) and issubclass(model, field.rel.to): fk_name = field.name _autoregister(admin, inline_model, follow=[fk_name]) if not inline_model._meta.get_field(fk_name).rel.is_hidden(): accessor = inline_model._meta.get_field(fk_name).remote_field.get_accessor_name() inline_fields.append(accessor) _autoregister(admin, model, inline_fields)
def field_is_reverse_relation(field): """ Takes a Model field and returns a Boolean indicating whether the field refers to a reverse relation. """ reverse_relations = (ForeignObjectRel, GenericRelation, ) return isinstance(field, reverse_relations)
def get_model_fields(self): """ Method to get all model fields for the content type associated with the forms specified content type :return: List of model field instances """ def is_valid_field(field): if isinstance(field, (models.AutoField, ForeignObjectRel, GenericRelation, GenericForeignKey)): return False else: return True return list(filter(is_valid_field, self.content_type.model_class()._meta.get_fields()))