我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用django.db.models.fields.related.ManyToOneRel()。
def check_reverse_relation(model, related_name): """ Helper method to check if a given model related_name pair return a reverse relation instance that is associated by a ForeignKey. model: subclass of django.db.models.Model related_name: str """ rel = get_reverse_relation(model, related_name) if rel is None: raise NotImplementedError( '{} does not have a reverse relation by the name "{}"'.format( model.__name__, related_name ) ) elif not isinstance(rel, ManyToOneRel): raise TypeError( 'The reverse relation "{}" on {}.{} is not a ForeignKey.' 'Timeseries only works with ForeignKey reverse relations.'.format( related_name, rel.field.model.__name__, rel.field.name ) ) return rel
def is_valid_field(self, model, field): """ Return true if the field exists within the model (or in the related model specified using the Django ORM __ notation) """ components = field.split('__', 1) try: field, _parent_model, _direct, _m2m = \ model._meta.get_field_by_name(components[0]) # reverse relation if isinstance(field, _RelatedObject): return self.is_valid_field(field.model, components[1]) # foreign key if field.rel and len(components) == 2: return self.is_valid_field(field.rel.to, components[1]) return True except FieldDoesNotExist: return False
def serializer_factory(endpoint=None, fields=None, base_class=None, model=None): if model is not None: assert endpoint is None, "You cannot specify both a model and an endpoint" from .endpoints import Endpoint endpoint = Endpoint(model=model) else: assert endpoint is not None, "You have to specify either a model or an endpoint" if base_class is None: base_class = endpoint.base_serializer meta_attrs = { 'model': endpoint.model, 'fields': fields if fields is not None else endpoint.get_fields_for_serializer() } meta_parents = (object, ) if hasattr(base_class, 'Meta'): meta_parents = (base_class.Meta, ) + meta_parents Meta = type('Meta', meta_parents, meta_attrs) cls_name = '{}Serializer'.format(endpoint.model.__name__) cls_attrs = { 'Meta': Meta, } for meta_field in meta_attrs['fields']: if meta_field not in base_class._declared_fields: try: model_field = endpoint.model._meta.get_field(meta_field) if isinstance(model_field, OneToOneRel): cls_attrs[meta_field] = serializers.PrimaryKeyRelatedField(read_only=True) elif isinstance(model_field, ManyToOneRel): cls_attrs[meta_field] = serializers.PrimaryKeyRelatedField(many=True, read_only=True) except FieldDoesNotExist: cls_attrs[meta_field] = serializers.ReadOnlyField() return type(cls_name, (NullToDefaultMixin, base_class, ), cls_attrs)
def get_field_names(model): '''Return a List of all fieldnames from a given model as strings sorted alphabetically.''' return sorted([f.name for f in model._meta.get_fields() # noqa if not isinstance(f, ManyToOneRel)])