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. 【Java_基础】java中static与final关键字的区别

    1.static关键字 经static关键字修饰的成员被该类的所有对象所共享,任意一对象对静态变量的修改其它对象都是可见的.通常通过类名来引用static成员.类加载的连接阶段将会为静态成员变量在jv ...

  2. nginx正则配置解释和fastadmin

    参考:http://www.cnblogs.com/netsa/p/6383094.html 1 2 3 4 5 6 7 8 9 10 11 1.^: 匹配字符串的开始位置:   2. $:匹配字符串 ...

  3. python爬虫基础03-requests库

    优雅到骨子里的Requests 本文地址:https://www.jianshu.com/p/678489e022c8 简介 上一篇文章介绍了Python的网络请求库urllib和urllib3的使用 ...

  4. fork()函数,一次调用,两次返回

    参考自:http://blog.csdn.net/dog_in_yellow/archive/2008/01/13/2041079.aspx 以前一直迷惑,什么叫一次调用,两次返回.通过上网搜索,终于 ...

  5. Codeforces Round #439 (Div. 2) B. The Eternal Immortality

    B. The Eternal Immortality 题目链接http://codeforces.com/contest/869/problem/B 解题心得:题意就是给出a,b,问(a!)/(b!) ...

  6. 【LeetCode】Path Sum(路径总和)

    这道题是LeetCode里的第112道题.是我在学数据结构——二叉树的时候碰见的题.题目要求: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和 ...

  7. 1 producer — n consumers 模型 实现

    #include<stdio.h> #include<string.h> #include<pthread.h> #include<stdlib.h> ...

  8. django基础(web框架,http协议,django安装)

    学习Django之前我们先来看什么是OSI七层模型: 应用层 表示层       应用层(五层模型中把这三层合成一个应用层) http协议 会话层 传输层                  提供端口对 ...

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

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

  10. POJ-2186 Popular Cows,tarjan缩点找出度为0的点。

    Popular Cows 题意:一只牛崇拜另外一只牛,这种崇拜关系可以传导.A->B,B->C =>A->C.现在给出所有的关系问你有多少牛被其他所有的牛都崇拜. 思路:就是一 ...