我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.db.models.ManyToOneRel()。
def _get_new_field_html(self, field_name): try: f, attr, value = lookup_field(field_name, self.org_obj, self) except (AttributeError, ObjectDoesNotExist): return EMPTY_CHANGELIST_VALUE else: allow_tags = False if f is None: allow_tags = getattr(attr, 'allow_tags', False) boolean = getattr(attr, 'boolean', False) if boolean: allow_tags = True text = boolean_icon(value) else: text = smart_text(value) else: if isinstance(f.rel, models.ManyToOneRel): field_val = getattr(self.org_obj, f.name) if field_val is None: text = EMPTY_CHANGELIST_VALUE else: text = field_val else: text = display_for_field(value, f) return mark_safe(text) if allow_tags else conditional_escape(text)
def _get_new_field_html(self, field_name): try: f, attr, value = lookup_field(field_name, self.org_obj, self) except (AttributeError, ObjectDoesNotExist): return EMPTY_CHANGELIST_VALUE else: allow_tags = False if f is None: allow_tags = getattr(attr, 'allow_tags', False) boolean = getattr(attr, 'boolean', False) if boolean: allow_tags = True text = boolean_icon(value) else: text = smart_unicode(value) else: if isinstance(f.rel, models.ManyToOneRel): field_val = getattr(self.org_obj, f.name) if field_val is None: text = EMPTY_CHANGELIST_VALUE else: text = field_val else: text = display_for_field(value, f) return mark_safe(text) if allow_tags else conditional_escape(text)
def get_field_docs(self, fieldname): try: field = self.model._meta.get_field(fieldname) if isinstance(field, models.ForeignKey): return _serializers[field.rel.to].docs() elif isinstance(field, models.ManyToManyField): spec = _serializers[field.rel.to].docs() spec['array'] = True return spec elif isinstance(field, models.ManyToOneRel): spec = _serializers[field.related_model].docs() spec['array'] = True return spec return FieldDocs(self.model, field) except models.FieldDoesNotExist: model = getattr(self.model, fieldname).related.related_model spec = _serializers[model].docs() spec['array'] = True return spec
def has_related_field_in_list_display(self): for field_name in self.list_display: try: field = self.lookup_opts.get_field(field_name) except FieldDoesNotExist: pass else: if isinstance(field.remote_field, models.ManyToOneRel): return True return False
def get_list_queryset(self): """ Get model queryset. The query has been filted and ordered. """ # First, get queryset from base class. queryset = self.queryset() # Use select_related() if one of the list_display options is a field # with a relationship and the provided queryset doesn't already have # select_related defined. if not queryset.query.select_related: if self.list_select_related: queryset = queryset.select_related() elif self.list_select_related is None: related_fields = [] for field_name in self.list_display: try: field = self.opts.get_field(field_name) except models.FieldDoesNotExist: pass else: if isinstance(field.rel, models.ManyToOneRel): related_fields.append(field_name) if related_fields: queryset = queryset.select_related(*related_fields) else: pass # Then, set queryset ordering. queryset = queryset.order_by(*self.get_ordering()) # Return the queryset. return queryset # List ordering
def init(self): self.label = label_for_field(self.field_name, self.obj.__class__, model_admin=self.admin_view, return_attr=False ) try: f, attr, value = lookup_field( self.field_name, self.obj, self.admin_view) except (AttributeError, ObjectDoesNotExist): self.text else: if f is None: self.allow_tags = getattr(attr, 'allow_tags', False) boolean = getattr(attr, 'boolean', False) if boolean: self.allow_tags = True self.text = boolean_icon(value) else: self.text = smart_text(value) else: if isinstance(f.rel, models.ManyToOneRel): self.text = getattr(self.obj, f.name) else: self.text = display_for_field(value, f) self.field = f self.attr = attr self.value = value
def has_related_field_in_list_display(self): for field_name in self.list_display: try: field = self.lookup_opts.get_field(field_name) except FieldDoesNotExist: pass else: if isinstance(field.remote_field, models.ManyToOneRel): # <FK>_id field names don't require a join. if field_name == field.get_attname(): continue return True return False
def init(self): self.label = label_for_field(self.field_name, self.obj.__class__, model_admin=self.admin_view, return_attr=False ) try: f, attr, value = lookup_field( self.field_name, self.obj, self.admin_view) except (AttributeError, ObjectDoesNotExist): self.text else: if f is None: self.allow_tags = getattr(attr, 'allow_tags', False) boolean = getattr(attr, 'boolean', False) if boolean: self.allow_tags = True self.text = boolean_icon(value) else: self.text = smart_unicode(value) else: if isinstance(f.rel, models.ManyToOneRel): self.text = getattr(self.obj, f.name) else: self.text = display_for_field(value, f) self.field = f self.attr = attr self.value = value
def has_related_field_in_list_display(self): for field_name in self.list_display: try: field = self.lookup_opts.get_field(field_name) except FieldDoesNotExist: pass else: if isinstance(field.rel, models.ManyToOneRel): return True return False
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_field_instance(self, model, field_name): field = model._meta.get_field(field_name) field_kwargs = {'model': model, 'name': field.name} if field.is_relation: if not field.related_model: # GenericForeignKey return if self.excluded(field.related_model): return field_cls = RelationField field_kwargs['related_model'] = field.related_model else: field_cls = self.get_field_cls(field) if isinstance(field, (ManyToOneRel, ManyToManyRel, GenericRel)): # Django 1.8 doesn't have .null attribute for these fields field_kwargs['nullable'] = True else: field_kwargs['nullable'] = field.null field_kwargs['suggest_options'] = ( field.name in self.suggest_options.get(model, []) ) field_instance = field_cls(**field_kwargs) # Check if suggested options conflict with field type if field_cls != StrField and field_instance.suggest_options: for option in field_instance.get_options(): if isinstance(option, text_type): # Convert to StrField field_instance = StrField(**field_kwargs) return field_instance
def get_reverse_fields(model, local_field_names): for name, attr in model.__dict__.items(): # Don't duplicate any local fields if name in local_field_names: continue # Django =>1.9 uses 'rel', django <1.9 uses 'related' related = getattr(attr, 'rel', None) or \ getattr(attr, 'related', None) if isinstance(related, models.ManyToOneRel): yield (name, related) elif isinstance(related, models.ManyToManyRel) and not related.symmetrical: yield (name, related)
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)