django 的model是如何把字段加入到meta中的
Posted pyhai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django 的model是如何把字段加入到meta中的相关的知识,希望对你有一定的参考价值。
def contribute_to_class(self, cls, name): self.set_attributes_from_name(name) self.model = cls cls._meta.add_field(self) if self.choices: setattr(cls, ‘get_%s_display‘ % self.name, curry(cls._get_FIELD_display, field=self))
上面在Field类中定义的contribute_to_class(self, cls, name),该类调用_meta.add_field(self)
def add_field(self, field): # Insert the given field in the order in which it was created, using # the "creation_counter" attribute of the field. # Move many-to-many related fields from self.fields into # self.many_to_many. if field.rel and isinstance(field.rel, ManyToManyRel): self.local_many_to_many.insert(bisect(self.local_many_to_many, field), field) if hasattr(self, ‘_m2m_cache‘): del self._m2m_cache else: self.local_fields.insert(bisect(self.local_fields, field), field) self.setup_pk(field) if hasattr(self, ‘_field_cache‘): del self._field_cache del self._field_name_cache if hasattr(self, ‘_name_map‘): del self._name_map
add_field会把field加到meta对象中。
fields = property(_fields) def get_fields_with_model(self): """ Returns a sequence of (field, model) pairs for all fields. The "model" element is None for fields on the current model. Mostly of use when constructing queries so that we know which model a field belongs to. """ try: self._field_cache except AttributeError: self._fill_fields_cache() return self._field_cache def _fill_fields_cache(self): cache = [] for parent in self.parents: for field, model in parent._meta.get_fields_with_model(): if model: cache.append((field, model)) else: cache.append((field, parent)) cache.extend([(f, None) for f in self.local_fields]) self._field_cache = tuple(cache) self._field_name_cache = [x for x, _ in cache]
调用meta的fields会返回model的所有field
model实例化要取得所有的field
以上是关于django 的model是如何把字段加入到meta中的的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Django 将表单/字段内容上传到 IPFS 节点?
如何将类、id、占位符属性添加到 django 模型表单中的字段