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}]>
django : related_name and related_query_name的更多相关文章
- related_name和related_query_name举例区别
例1: class UserInfo(models.Model): nickname = models.CharField(max_length=32) username = models.CharF ...
- Django 1.10 中文文档------3.2.1 模型Models
3.2.1 models模型 通常一个模型映射一张单独的数据表. 基本概念: 每个model都是django.db.models.Model的子类 model的每个属性代表数据表的某一列 Django ...
- django 外键 ,django __
data sqlite> select * from author; id name age 1 jim 12 2 tom 11 sqlite> select * from book; i ...
- Django自定义用户认证系统Customizing authentication
扩展已有的用户模型Extending the existing User model 有两种方法来扩展默认的User Model而不用重写自己的模型.如果你不需要改变存储在数据库中的字段,而只是需要改 ...
- Django用户认证系统(三)组与权限
Django的权限系统很简单,它可以赋予users或groups中的users以权限. Django admin后台就使用了该权限系统,不过也可以用到你自己的代码中. User对象具有两个ManyTo ...
- Django用户认证系统(一)User对象
User对象 User对象是认证系统的核心.用户对象通常用来代表网站的用户,并支持例如访问控制.注册用户.关联创建者和内容等.在Django认证框架中只有一个用户类,例如超级用户('superuser ...
- 关系类型字段 -- Django从入门到精通系列教程
该系列教程系个人原创,并完整发布在个人官网刘江的博客和教程 所有转载本文者,需在顶部显著位置注明原作者及www.liujiangblog.com官网地址. Python及Django学习QQ群:453 ...
- 模型的继承 -- Django从入门到精通系列教程
该系列教程系个人原创,并完整发布在个人官网刘江的博客和教程 所有转载本文者,需在顶部显著位置注明原作者及www.liujiangblog.com官网地址. Python及Django学习QQ群:453 ...
- Django Model field reference
===================== Model field reference ===================== .. module:: django.db.models.field ...
随机推荐
- ios多线程之GCD
介绍: Grand Central Dispatch 简称(GCD)是苹果公司开发的技术,以优化的应用程序支持多核心处理器和其他的对称多处理系统的系统.这建立在任务并行执行的线程池模式的基础上的.它首 ...
- POJ 3080 Blue Jeans、POJ 3461 Oulipo——KMP应用
题目:POJ3080 http://poj.org/problem?id=3080 题意:对于输入的文本串,输出最长的公共子串,如果长度相同,输出字典序最小的. 这题数据量很小,用暴力也是16ms,用 ...
- [JOY]1143 飘飘乎居士的约会
题目描述 又是美妙的一天,这天飘飘乎居士要和MM约会,因此他打扮的格外帅气.但是,因为打扮的时间花了太久,离约会的时间已经所剩无几. 幸运的是,现在飘飘乎居士得到了一张nm的地图,图中左上角是飘飘乎居 ...
- [LUOGU] 1090 合并果子
题目描述 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和.可 ...
- 【OS_Linux】Linux中虚拟机的三种上网方式——桥接、NAT、Host-only
1.桥接 桥接方便做实验,配置ip方便.可以和局域网中的其他机器进行通信,也可以和公网进行通信.缺点是会占用主机所在局域网的一个ip. 2. NAT NAT模式下虚拟机可以和主机进行通信,可以上网,而 ...
- jsDate()
var myTime=new Date();//myTime的数据类型为(typeof) object //下面得到的都为number 类型 getFullYear();年 四位数字返回年份. get ...
- 【css】报错,错误代码77,CURLE_SSL_CACERT_BADFILE (77)解决方法
CURLE_SSL_CACERT_BADFILE (77) - 读取 SSL CA 证书时遇到问题(可能是路径错误或访问权限问题) 在微信接口相关开发时容易出现此问题 这一般是因为服务更新了相关的软件 ...
- day12-图
- Spring核心技术(十二)——基于Java的容器配置(二)
使用@Configuration注解 @Configuration注解是一个类级别的注解,表明该对象是用来指定Bean的定义的.@Configuration注解的类通过@Bean注解的方法来声明Bea ...
- http协议工作原理(转)
WWW是以Internet作为传输媒介的一个应用系统,WWW网上最基本的传输单位是Web网页.WWW的工作基于客户机/服务器计算模型,由Web 浏览器(客户机)和Web服务器(服务器)构成,两者之间 ...