django之关联field 描述子是如何实现的
model定义时,每个field都是一个类属性,一个对象。在生成类时,属性有contribute_to_class的方法,会调用该方法。
m2m field,它会先调用自己的contribute_to_class,然后调用父类的contribute_to_class,因为m2m类是继承自relatfield,
def contribute_to_class(self, cls, name, virtual_only=False): super(RelatedField, self).contribute_to_class(cls, name, virtual_only=virtual_only)#在option添加一个field self.opts = cls._meta if not cls._meta.abstract:
if self.remote_field.related_name:
related_name = force_text(self.remote_field.related_name) % {
'class': cls.__name__.lower(),
'app_label': cls._meta.app_label.lower()
}
self.remote_field.related_name = related_name def resolve_related_class(model, related, field):
field.remote_field.model = related
field.do_related_class(related, model)
lazy_related_operation(resolve_related_class, cls, self.remote_field.model, field=self)
def do_related_class(self, other, cls):#关键是调用这个函数时把两个model类调换了,model与related位置对调。
self.set_attributes_from_rel()
self.contribute_to_related_class(other, self.remote_field) def lazy_related_operation(function, model, *related_models, **kwargs):
"""
Schedule `function` to be called once `model` and all `related_models`
have been imported and registered with the app registry. `function` will
be called with the newly-loaded model classes as its positional arguments,
plus any optional keyword arguments. The `model` argument must be a model class. Each subsequent positional
argument is another model, or a reference to another model - see
`resolve_relation()` for the various forms these may take. Any relative
references will be resolved relative to `model`. This is a convenience wrapper for `Apps.lazy_model_operation` - the app
registry model used is the one found in `model._meta.apps`.
"""
models = [model] + [resolve_relation(model, rel) for rel in related_models]
model_keys = (make_model_tuple(m) for m in models)
apps = model._meta.apps
return apps.lazy_model_operation(partial(function, **kwargs), *model_keys) def lazy_model_operation(self, function, *model_keys):
"""
Take a function and a number of ("app_label", "modelname") tuples, and
when all the corresponding models have been imported and registered,
call the function with the model classes as its arguments. The function passed to this method must accept exactly n models as
arguments, where n=len(model_keys).
"""
# If this function depends on more than one model, we recursively turn
# it into a chain of functions that accept a single model argument and
# pass each in turn to lazy_model_operation.
model_key, more_models = model_keys[0], model_keys[1:]
if more_models:
supplied_fn = function def function(model):
next_function = partial(supplied_fn, model)
# Annotate the function with its field for retrieval in
# migrations.state.StateApps.
if getattr(supplied_fn, 'keywords', None):
next_function.field = supplied_fn.keywords.get('field')
self.lazy_model_operation(next_function, *more_models) # If the model is already loaded, pass it to the function immediately.
# Otherwise, delay execution until the class is prepared.
try:
model_class = self.get_registered_model(*model_key)
except LookupError:
self._pending_operations[model_key].append(function)
else:
function(model_class)
django之关联field 描述子是如何实现的的更多相关文章
- django之关联field 描述子
"""Accessors for related objects. When a field defines a relation between two models, ...
- django之relacted.py(探秘django的关联field)
生成model类对象时,传入的每个field对象都会调用其contribute_to_class函数,生成对应的属性. def contribute_to_class(self, cls, name, ...
- django自关联,auth模块
一.自关联 写蛮好的一篇博客:https://www.cnblogs.com/Kingfan1993/p/9936541.html 1.一对多关联 1.表内自关联是指表内数据相关联的对象和表是相同字段 ...
- BRIEF 特征描述子
Binary Robust Independent Elementary Features www.cnblogs.com/ronny 1. BRIEF的基本原理 我们已经知道SIFT特征采用了128 ...
- 基于HOG-3D的时空描述子
作者提出一种新的基于局部描述子的行为识别算法.
- Brief描述子
一.Brief算法 1.基本原理 BRIEF是2010年的一篇名为<BRIEF:Binary Robust Independent Elementary Features>的文章中提出,B ...
- SIFT算法:特征描述子
SIFT算法:DoG尺度空间生产 SIFT算法:KeyPoint找寻.定位与优化 SIFT算法:确定特征点方向 SIFT算法:特征描述子 目录: 1.确定描述子采样区域 2.生成描述子 2.1 旋 ...
- SIFT解析(三)生成特征描述子
以上两篇文章中检测在DOG空间中稳定的特征点,lowe已经提到这些特征点是比Harris角点等特征还要稳定的特征.下一步骤我们要考虑的就是如何去很好地描述这些DOG特征点. 下面好好说说如何来描述这些 ...
- (二)ORB描述子提取源码思路与实现
ORBSLAM2中ORB特征提取的特点 ORBSLAM2中通过对OpenCV中的ORB特征点提取类进行修改,对图像进行分块提取,而后划分节点,使得每个节点中保存的特征点性能是该节点所有特征点中最好的. ...
随机推荐
- Java Web 学习笔记 1
Java Web 学习笔记 1 一.Web开发基础 1-1 Java Web 应用开发概述 1.1.1 C/S C/S(Client/Server)服务器通常采用高性能的PC机或工作站,并采用大型数据 ...
- ConcurrentDictionary对象
ConcurrentDictionary<int, List<a>> dic = new ConcurrentDictionary<int, List<a>& ...
- 软件开发者路线图梗概&书摘chapter7
软件工程是一门技艺 原因:理解不足以系统化 技能:为交付可以工作的软件 重要的原因:了解不足以将它写成可供别人直接运用并得到相同成果的格式 大多数程序员低于平均水平 工艺:一套高度重视技能的训练和传统 ...
- android开发解决Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'. > java.lang.RuntimeException: java.lang.RuntimeException: c.....
网上常见的方法我都试过,都没能解决,偶然看到的一个方法解决了,在这了记录一下. 在App目录下的build.gradle的android{ ... ....}中添加如下代码,即可解决.(xx.xx. ...
- Assembly Experiment5
Answer to the experiment(1),(2),(3),(4) Experiment(5): Screenshots&Results: from the command u w ...
- 关于定时器setTimeout()方法的实践--巧解bug
_使用开发环境:UAP:_ _框架:JQuery.MX:_ 最近的开发的页面中,有一处需要在提交的 datagrid里启用行编辑,就会发生奇怪的bug,编辑过程中如图所示不移开焦点直接点保存,那么已输 ...
- day02 进制之间的转换and计算机编码叙述
一. 进制的转好技巧 二进制:0 1 八进制:0 1 2 3 4 5 6 7 十进制:0 1 2 3 4 5 6 7 8 9 十六进制:0 1 2 3 4 5 6 7 8 9 A B C D E ...
- MySQL高可用工具--orchestrator
orchestrator是一款MySQL高可用工具,其支持: 集群拓扑探测 集群拓扑重塑 故障恢复 提供3种使用方式: 命令行 HTTP API web页面 orchestator github地址
- C# .NET newtonsoft.json 多版本冲突解决
A.DLL 引用了6.0 的 newtonsoft.json (V2 运行时),B.DLL 引用了10.0 的 newtonsoft.json (V4 运行时). 可以在.CONFIG RUNTIM ...
- python3-基础6
函数嵌套调用: 再调用一个函数的过程中,又调用了其他的函数 函数的嵌套定义:在一个函数的内部,又定义另外一个函数,函数内部定义的变量,在外部不能被调用 名称空间:一种隔离的概念,专门用来存放名字的地方 ...