分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

django 的model是如何把字段加入到meta中的

发布时间:2023-09-06 02:02责任编辑:熊小新关键词:djangometa
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中的

原文地址:https://www.cnblogs.com/pyhai/p/9245047.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved