我们从Python开源项目中,提取了以下37个代码示例,用于说明如何使用django.db.models.fields.related.ManyToManyField()。
def get_all_related_objects(self, local_only=False, include_hidden=False, include_proxy_eq=False): include_parents = True if local_only is False else PROXY_PARENTS fields = self._get_fields( forward=False, reverse=True, include_parents=include_parents, include_hidden=include_hidden, ) fields = (obj for obj in fields if not isinstance(obj.field, ManyToManyField)) if include_proxy_eq: children = chain.from_iterable(c._relation_tree for c in self.concrete_model._meta.proxied_children if c is not self) relations = (f.remote_field for f in children if include_hidden or not f.remote_field.field.remote_field.is_hidden()) fields = chain(fields, relations) return list(fields)
def get_all_related_objects(self, local_only=False, include_hidden=False, include_proxy_eq=False): include_parents = True if local_only is False else PROXY_PARENTS fields = self._get_fields( forward=False, reverse=True, include_parents=include_parents, include_hidden=include_hidden, ) fields = (obj for obj in fields if not isinstance(obj.field, ManyToManyField)) if include_proxy_eq: children = chain.from_iterable(c._relation_tree for c in self.concrete_model._meta.proxied_children if c is not self) relations = (f.rel for f in children if include_hidden or not f.rel.field.rel.is_hidden()) fields = chain(fields, relations) return list(fields)
def model_to_dict(instance, fields=None, exclude=None): """ Returns a dict containing the data in ``instance`` suitable for passing as a Form's ``initial`` keyword argument. ``fields`` is an optional list of field names. If provided, only the named fields will be included in the returned dict. ``exclude`` is an optional list of field names. If provided, the named fields will be excluded from the returned dict, even if they are listed in the ``fields`` argument. """ # avoid a circular import from django.db.models.fields.related import ManyToManyField opts = instance._meta data = {} for f in chain(opts.concrete_fields, opts.virtual_fields, opts.many_to_many): if not getattr(f, 'editable', False): continue if fields and f.name not in fields: continue if exclude and f.name in exclude: continue if isinstance(f, ManyToManyField): # If the object doesn't have a primary key yet, just use an empty # list for its m2m fields. Calling f.value_from_object will raise # an exception. if instance.pk is None: data[f.name] = [] else: # MultipleChoiceWidget needs a list of pks, not object instances. qs = f.value_from_object(instance) if qs._result_cache is not None: data[f.name] = [item.pk for item in qs] else: data[f.name] = list(qs.values_list('pk', flat=True)) else: data[f.name] = f.value_from_object(instance) return data
def get_all_related_many_to_many_objects(self, local_only=False): include_parents = True if local_only is not True else PROXY_PARENTS fields = self._get_fields( forward=False, reverse=True, include_parents=include_parents, include_hidden=True ) return [obj for obj in fields if isinstance(obj.field, ManyToManyField)]
def get_all_related_m2m_objects_with_model(self): fields = self._get_fields(forward=False, reverse=True, include_hidden=True) return [self._map_model(obj) for obj in fields if isinstance(obj.field, ManyToManyField)]
def contribute_to_class(self, cls, name, **kwargs): if not self.sorted: return super(SortedManyToManyField, self).contribute_to_class(cls, name, **kwargs) # To support multiple relations to self, it's useful to have a non-None # related name on symmetrical relations for internal reasons. The # concept doesn't make a lot of sense externally ("you want me to # specify *what* on my non-reversible relation?!"), so we set it up # automatically. The funky name reduces the chance of an accidental # clash. if self.rel.symmetrical and (self.rel.to == "self" or self.rel.to == cls._meta.object_name): self.rel.related_name = "%s_rel_+" % name super(ManyToManyField, self).contribute_to_class(cls, name, **kwargs) # The intermediate m2m model is not auto created if: # 1) There is a manually specified intermediate, or # 2) The class owning the m2m field is abstract. if not self.rel.through and not cls._meta.abstract: self.rel.through = self.create_intermediate_model(cls) # Add the descriptor for the m2m relation setattr(cls, self.name, SortedManyToManyDescriptor(self)) # Set up the accessor for the m2m table name for the relation self.m2m_db_table = curry(self._get_m2m_db_table, cls._meta) # Populate some necessary rel arguments so that cross-app relations # work correctly. if isinstance(self.rel.through, six.string_types): def resolve_through_model(field, model, cls): field.rel.through = model add_lazy_relation(cls, self, self.rel.through, resolve_through_model) if hasattr(cls._meta, 'duplicate_targets'): # Django<1.5 if isinstance(self.rel.to, six.string_types): target = self.rel.to else: target = self.rel.to._meta.db_table cls._meta.duplicate_targets[self.column] = (target, "m2m")
def get_internal_type(self): return 'ManyToManyField'
def choices(self, cl): from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE yield { 'selected': self.lookup_val is None and not self.lookup_val_isnull, 'query_string': cl.get_query_string({}, [self.lookup_kwarg, self.lookup_kwarg_isnull]), 'display': _('All'), } for pk_val, val in self.lookup_choices: yield { 'selected': self.lookup_val == smart_text(pk_val), 'query_string': cl.get_query_string({ self.lookup_kwarg: pk_val, }, [self.lookup_kwarg_isnull]), 'display': val, } if (isinstance(self.field, ForeignObjectRel) and (self.field.field.null or isinstance(self.field.field, ManyToManyField)) or hasattr(self.field, 'rel') and (self.field.null or isinstance(self.field, ManyToManyField))): yield { 'selected': bool(self.lookup_val_isnull), 'query_string': cl.get_query_string({ self.lookup_kwarg_isnull: 'True', }, [self.lookup_kwarg]), 'display': EMPTY_CHANGELIST_VALUE, }
def choices(self, cl): # #### MPTT ADDITION START try: # EMPTY_CHANGELIST_VALUE has been removed in django 1.9 from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE except: EMPTY_CHANGELIST_VALUE = self.empty_value_display # #### MPTT ADDITION END yield { 'selected': self.lookup_val is None and not self.lookup_val_isnull, 'query_string': cl.get_query_string({}, [self.lookup_kwarg, self.lookup_kwarg_isnull]), 'display': _('All'), } for pk_val, val, padding_style in self.lookup_choices: yield { 'selected': self.lookup_val == smart_text(pk_val), 'query_string': cl.get_query_string({ self.lookup_kwarg: pk_val, }, [self.lookup_kwarg_isnull]), 'display': val, # #### MPTT ADDITION START 'padding_style': padding_style, # #### MPTT ADDITION END } if (isinstance(self.field, ForeignObjectRel) and (self.field.field.null or isinstance(self.field.field, ManyToManyField)) or hasattr(self.field, 'rel') and (self.field.null or isinstance(self.field, ManyToManyField))): yield { 'selected': bool(self.lookup_val_isnull), 'query_string': cl.get_query_string({ self.lookup_kwarg_isnull: 'True', }, [self.lookup_kwarg]), 'display': EMPTY_CHANGELIST_VALUE, }
def model_to_dict(instance, fields=None, exclude=None): """ Returns a dict containing the data in ``instance`` suitable for passing as a Form's ``initial`` keyword argument. ``fields`` is an optional list of field names. If provided, only the named fields will be included in the returned dict. ``exclude`` is an optional list of field names. If provided, the named fields will be excluded from the returned dict, even if they are listed in the ``fields`` argument. """ # avoid a circular import from django.db.models.fields.related import ManyToManyField opts = instance._meta data = {} for f in opts.concrete_fields + opts.many_to_many: if not f.editable: continue if fields and not f.name in fields: continue if exclude and f.name in exclude: continue if isinstance(f, ManyToManyField): # If the object doesn't have a primary key yet, just use an empty # list for its m2m fields. Calling f.value_from_object will raise # an exception. if instance.pk is None: data[f.name] = [] else: # MultipleChoiceWidget needs a list of pks, not object instances. data[f.name] = list(f.value_from_object(instance).values_list('pk', flat=True)) else: data[f.name] = f.value_from_object(instance) return data