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 描述子是如何实现的的更多相关文章

  1. django之关联field 描述子

    """Accessors for related objects. When a field defines a relation between two models, ...

  2. django之relacted.py(探秘django的关联field)

    生成model类对象时,传入的每个field对象都会调用其contribute_to_class函数,生成对应的属性. def contribute_to_class(self, cls, name, ...

  3. django自关联,auth模块

    一.自关联 写蛮好的一篇博客:https://www.cnblogs.com/Kingfan1993/p/9936541.html 1.一对多关联 1.表内自关联是指表内数据相关联的对象和表是相同字段 ...

  4. BRIEF 特征描述子

    Binary Robust Independent Elementary Features www.cnblogs.com/ronny 1. BRIEF的基本原理 我们已经知道SIFT特征采用了128 ...

  5. 基于HOG-3D的时空描述子

    作者提出一种新的基于局部描述子的行为识别算法.

  6. Brief描述子

    一.Brief算法 1.基本原理 BRIEF是2010年的一篇名为<BRIEF:Binary Robust Independent Elementary Features>的文章中提出,B ...

  7. SIFT算法:特征描述子

    SIFT算法:DoG尺度空间生产  SIFT算法:KeyPoint找寻.定位与优化 SIFT算法:确定特征点方向  SIFT算法:特征描述子 目录: 1.确定描述子采样区域 2.生成描述子 2.1 旋 ...

  8. SIFT解析(三)生成特征描述子

    以上两篇文章中检测在DOG空间中稳定的特征点,lowe已经提到这些特征点是比Harris角点等特征还要稳定的特征.下一步骤我们要考虑的就是如何去很好地描述这些DOG特征点. 下面好好说说如何来描述这些 ...

  9. (二)ORB描述子提取源码思路与实现

    ORBSLAM2中ORB特征提取的特点 ORBSLAM2中通过对OpenCV中的ORB特征点提取类进行修改,对图像进行分块提取,而后划分节点,使得每个节点中保存的特征点性能是该节点所有特征点中最好的. ...

随机推荐

  1. SQL group_concat find_in_set 的使用

    SELECT p.id as pid,p.code as code,p.topic_name topic_name,p.vm_id as vm_id,GROUP_CONCAT(iso.iso_name ...

  2. 第3章 Data语意学

    在C++中经常会遇到一个类的大小问题,关于一个类的大小一般受到三个方面的影响. 语言本身所造成的额外负担,如在虚拟继承中会遇到如派生类中会包含一个指针指向base class subobjec,这样会 ...

  3. 加载Excel时,如何过滤空行空列

    巨硬为Excel提供了丰富的C#接口,基本上可以将Excel当做一个微型数据库来用,奈何前端的我们,sql也只会写两句select * from table: 目前工作中遇到了一个问题,在需要读取的E ...

  4. 14. Encryption tools (加密工具 8个)

    SSH(Secure Shell)现在是无处不在的程序,用于在远程机器上登录或执行命令. 它通过不安全的网络在两个不受信任的主机之间提供安全的加密通信,取代了可怕的不安全的telnet / rlogi ...

  5. Python 安装beautifulsoup4遇到No module named setuptools问题解决方法

    背景说明: 电脑win7-32 在Python 3.3.5下安装beautifulsoup4 4.6.0(下载链接https://pypi.org/project/beautifulsoup4/#fi ...

  6. java8_api_math

    java.math包    BigDecimal    BigInteger    MathContext    RoundingMode这是枚举        BigDecimal        不 ...

  7. SMD 自动点料机维修

    SMD 自动点料机维修 这个工具是一个好帮手,但是过完年回来发现坏了. 设置了数量不会自动停,按停止键没有反应,一定要按打印键才能停止. 这可愁死我了. 正常情况下开机设置好数量,然后开始点数,点到数 ...

  8. 环境准备阶段--搭建oracle linux 6.5系统

    环境准备阶段--搭建oracle linux 6.5系统 选择9.x版本兼容性 分配6GB内存 再次重启可以进入桌面,提示如下,勾选然后close 配置主机名,网络.hosts [root@ocp-1 ...

  9. python selenium-webdriver 元素操作之鼠标操作(四)

    上节内容主要说明了元素的定位,本节内容说要说对元素的操作,元素的操作分为两部分一部分是鼠标的操作,另一种是对键盘对元素的操作,下面我们主要讲解一下鼠标对元素的操作. webdriver 模块中几种比较 ...

  10. 【java】java三种技术架构

    JAVAEE : 企业环境下开发应用程序,servlet,jsp,针对web应用程序的开发. JAVASE:普通简单的桌面应用程序,商务应用程序.(java版扫雷) JAVAME手机应用软件,电子消费 ...