django : related_name and related_query_name
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}]>
最新文章
- ThinkPHP3.2.3整合smarty模板(一)
- 文件属性之setuid位
- 持续集成基础-Jenkins(一)
- C#窗体 WinForm 对话框,流
- 7、SpringMVC源码分析(2):分析HandlerAdapter.handle方法,了解handler方法的调用细节以及@ModelAttribute注解
- 跟着PHP100第一季学写一个CMS(1-10)
- ios 复制黏贴板的使用
- 最全CSS3选择器
- 九、VueJs 填坑日记之在项目中使用jQuery
- Kubernetes 使用私服镜像
- ios手机录屏软件哪个好
- 使用Vlc.DotNet打开摄像头并截图 C#
- 原生Ajax--XmlHttpRequest对象和jQuery.ajax()
- 读取文件不是真实的具体路径 setZh.ini
- Python学习手记
- 【读书笔记】iOS-iCloud介绍
- CentOS7配置crate集群
- CF838D Airplane Arrangements
- [C语言] 数据结构-算法效率的度量方法-事前分析估算方法
- Tarjan算法初探 (1):Tarjan如何求有向图的强连通分量
热门文章
- C# 读App.config配置文件[2]: .Net Core框架
- Idea 搭建Maven--web项目(MVC)
- 条款37:绝不重新定义继承而来的缺省参数值(Never redefine a function&#39;s inherited default parameter value)
- Python基础:列表(list)和元组(tuple)
- python项目开发视频
- 关于json的dump和dumps
- PAT Basic 1023
- Java-终止应用程序
- luogu3159 [CQOI2012]交换棋子
- [android开发篇] api demo 官网学习网址