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. (49)zabbix事件是什么?事件来源有哪些分类

    什么是zabbix 事件 在trigger的文章内,我们已经有用到事件,这个事件要讲概念真心不知道怎么说,就拿trigger事件来说,如果trigger从当前值ok转变为problem,那么我们称之为 ...

  2. MariaDB数据库(五)

    1. MariaDB主从架构 1.1 概述 主从架构用来预防数据丢失.主从多用于网站架构,因为主从的同步机制是异步的,数据的同步有一定延迟,也就是说有可能会造成数据的丢失,但是性能比较好,因此网站大多 ...

  3. Linux内核 ——进程管理之进程诞生(基于版本4.x)

    <奔跑吧linux内核>3.1笔记,不足之处还望大家批评指正 进程是Linux内核最基本的抽象之一,它是处于执行期的程序.它不仅局限于一段可执行代码(代码段),还包括进程需要的其他资源.在 ...

  4. java 之Thread线程相关yield()、sleep()、wait()、join()、run和start方法详解

    1.sleep() 使当前线程(即调用该方法的线程)暂停执行一段时间,让其他线程有机会继续执行,但它并不释放对象锁.也就是说如果有synchronized同步快,其他线程仍然不能访问共享数据.注意该方 ...

  5. JavaScript正则表达式-RegExp对象

    RegExp对象方法 exec():与String对象的match()方法功能相同. 参数为被搜索字符串.返回数组或null. test():与String对象的search()方法功能相同. 参数为 ...

  6. PAT Basic 1014

    1014 福尔摩斯的约会 大侦探福尔摩斯接到一张奇怪的字条:“我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm” ...

  7. 10大mysql需要注意的参数

    MySQL变量很多,其中有一些MySQL变量非常值得我们注意,下面就为您介绍一些值得我们重点学习的MySQL变量,供您参考. 1 Threads_connected 首先需要注意的,想得到这个变量的值 ...

  8. Java设计模式学习三-----工厂模式

    工厂模式 工厂模式(Factory Pattern)是Java中最常用的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 在工厂模式中,创建对象时不会对客户端暴露创建逻 ...

  9. php在字符串中替换多个字符

    php替换多个字符串str_replace函数 echo str_replace(array("m","i"),array("n",&quo ...

  10. BNUOJ 19297 Code Refactoring

    Code Refactoring Time Limit: 3000ms Memory Limit: 131072KB   This problem will be judged on UVA. Ori ...