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 ...
随机推荐
- PAT 乙级 1011
题目 题目地址:PAT 乙级 1011 思路 这道题的比较坑的地方在于给定数据的范围 int 类型的数据大小是[-2^31 , 2^31 -1] 即 [-2147483648,2147483647] ...
- UVa-213-信息解码
这题的话,我们只要理解题意,应该就不算很难. 我们可以开一个二维数组,用来存放对应的编码字符,第一个下表是length,第二个下标是value,这样一来,我们在读入数据的时候就进行处理,然后想要使用的 ...
- Linux基础学习-Postfix与Dovecot部署邮件系统
电子邮件系统 电子邮件系统是我们在日常工作.生活中最常用的一种网络服务. 部署基础的电子邮件系统 [root@qdlinux ~]# yum install bind-chroot -y [root@ ...
- perl学习之:localtime
Perl中localtime()函数以及sprintf (2011-4-25 19:39)localtime函数 localtime函数,根据它所在的上下文,可以用两种完全不同的方法来运行.在标量上下 ...
- perl学习之:正则表达式
- PHP方法之 substr
简单描述: substr 主要用于字符串的截取,但是不适用于中文字符串,易出现乱码,中文字符串可使用mbstring. 方法申明: substr(string,start,length) string ...
- 【java】 field 和 variable 区别及相关术语解释
Having said that, the remainder of this tutorial uses the following general guidelines when discussi ...
- vs实用插件
Live Share 强烈推荐的一款插件,能在VS程序中打开文件并且显示他的效果.非常非常实用!,具体功能介绍在你搜索该插件时候有说明,非常非常好用的一款插件! 后续插件推荐转载参考与其他博主 1.C ...
- Google 超分辨率技术 RAISR
每天都有数以百万计的图片在网络上被分享.储存,用户借此探索世界,研究感兴趣的话题,或者与朋友家人分享假期照片.问题是,大量的图片要嘛被照相设备的像素所限制,要嘛在手机.平板或网络限制下被人为压缩,降低 ...
- ajax dataType
dataType 类型:String 预期服务器返回的数据类型.如果不指定,jQuery 将自动根据 HTTP 包 MIME 信息来智能判断,比如 XML MIME 类型就被识别为 XML.在 1.4 ...