This post is about two Django ForeignKey parameters

related_name
related_query_name

See an example below

class Cluster(models.Model):
_id = models.UUIDField(unique=True, null=False, default=uuid.uuid1)
name = models.CharField(max_length=200, unique=True, null=False) class Node(models.Model):
_id = models.UUIDField(unique=True, null=False, default=uuid.uuid1)
name = models.CharField(max_length=200, unique=True, null=False)
cluster = models.ForeignKey(
Cluster,
on_delete=models.PROTECT,
to_field='_id',
db_constraint=False
)

We did not set relatd_name and related_query_name here, so django will use the default. Related_name will be node_set (mode name with a _set) and related_query_name will be node(model name).

The value of the two parameter is not important. What we care about is the usage of this two parameters.

To discuss that, we need to find a way to call this two types of models. Here we call the model with foreign key 'slave_model' and the other model 'master_model'.

The related_name is used for the master_model object to refer back to slave_models. For example:

>>> for node_obj in  c1.node_set.all():
... print(node_obj.cluster_id)
...
26f0655e-bf2b-11e8-8a30-f000ac192ced
26f0655e-bf2b-11e8-8a30-f000ac192ced
>>> c1._id
UUID('26f0655e-bf2b-11e8-8a30-f000ac192ced')

The c1 is a master_model object. The related_name is now one attribute of it. With this attr, master_model object can refer back to slave_models.

The related_query_name usually used in two scenario.

First, related_query_name used in filter. Lets see an example

Cluster.objects.filter(key=value)

Usually, the key should be column of cluster. With related_query_name , the key could be column of node. For example:

>>> Cluster.objects.filter(node__name=n1.name)
<QuerySet [<Cluster: Cluster object (4)>]>

Please note that the node is the related_query_name and we always add two "_" between related_query_name and column name

Second usage is

Cluster.objects.filter(node__name=n1.name).values('column', 'column')

Normally, you can specify the column of cluster to get the target column you want. With related_query_name , you can specify the column of node (slave_model).

>>> Cluster.objects.filter(node__name=n1.name).values('name', 'node__name', 'node__pk')
<QuerySet [{'name': 'c1', 'node__name': 'n1', 'node__pk': 5}]>

django : related_name and related_query_name的更多相关文章

  1. related_name和related_query_name举例区别

    例1: class UserInfo(models.Model): nickname = models.CharField(max_length=32) username = models.CharF ...

  2. Django 1.10 中文文档------3.2.1 模型Models

    3.2.1 models模型 通常一个模型映射一张单独的数据表. 基本概念: 每个model都是django.db.models.Model的子类 model的每个属性代表数据表的某一列 Django ...

  3. django 外键 ,django __

    data sqlite> select * from author; id name age 1 jim 12 2 tom 11 sqlite> select * from book; i ...

  4. Django自定义用户认证系统Customizing authentication

    扩展已有的用户模型Extending the existing User model 有两种方法来扩展默认的User Model而不用重写自己的模型.如果你不需要改变存储在数据库中的字段,而只是需要改 ...

  5. Django用户认证系统(三)组与权限

    Django的权限系统很简单,它可以赋予users或groups中的users以权限. Django admin后台就使用了该权限系统,不过也可以用到你自己的代码中. User对象具有两个ManyTo ...

  6. Django用户认证系统(一)User对象

    User对象 User对象是认证系统的核心.用户对象通常用来代表网站的用户,并支持例如访问控制.注册用户.关联创建者和内容等.在Django认证框架中只有一个用户类,例如超级用户('superuser ...

  7. 关系类型字段 -- Django从入门到精通系列教程

    该系列教程系个人原创,并完整发布在个人官网刘江的博客和教程 所有转载本文者,需在顶部显著位置注明原作者及www.liujiangblog.com官网地址. Python及Django学习QQ群:453 ...

  8. 模型的继承 -- Django从入门到精通系列教程

    该系列教程系个人原创,并完整发布在个人官网刘江的博客和教程 所有转载本文者,需在顶部显著位置注明原作者及www.liujiangblog.com官网地址. Python及Django学习QQ群:453 ...

  9. Django Model field reference

    ===================== Model field reference ===================== .. module:: django.db.models.field ...

随机推荐

  1. 更改ubuntu的官方镜像源

    我们自己安装的ubuntu通常默认镜像源是官方的,并不好用,因为网速以及限制比较多,所以为了使用方便,通常都会去更改一下默认的镜像源配置. 这里我们使用清华大学开源镜像软件站,https://mirr ...

  2. (51)zabbix命令:zabbix_get获取item数据

    zabbix_get是什么?有什么作用? 总有人在群里提问,为什么zabbix获取不到数据,为什么zabbix提示Not Support,怎么办?别老问,用zabbix_get试着获取数据即可.在za ...

  3. memory bist lib

    model NVIC_REG6T_1024x32(resetb,margin,clk,en,we,addr,d,q) ( bist_definition( clock clk high; chip_e ...

  4. systemverilog(3)之Randomize

    what to randomize? (1) primary input data <==one data (2)encapsulated input data <== muti grou ...

  5. PyQt5(1)——QToolTip, QPushButton, QMessageBox, QDesktopWidget

    #面向对象方法 import sys from PyQt5.QtWidgets import QApplication, QWidget, QToolTip, QPushButton, QMessag ...

  6. LeetCode(8)String to Integer (atoi)

    题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  7. stm32L011F3——串口实例

    /* STM32L0xx HAL library initialization: - Configure the Flash prefetch, Flash preread and Buffer ca ...

  8. Experimental considerations

    先知 重金属颗粒与气孔大小 气孔位置.密度与开合时间 角质层厚度 意外 叶子第二天掉落 样本没有放冰箱 高光谱仪器损坏 天气下雨/雪 仪器预约 楼顶/实验室门卡提前一天预约 光合仪提前一天预约 ASD ...

  9. ASP.NET中一般处理程序报的错误:由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值

    1.把context.Response.End();代码换成 HttpContext.Current.ApplicationInstance.CompleteRequest(); 2.把context ...

  10. UTF-8转字典

      NSString *textStr = ] forKey:[string substringToIndex:range.location]];     }];     NSLog(@"% ...