[django]Django外键(ForeignKey)操作以及related_name的作用
https://blog.csdn.net/hpu_yly_bj/article/details/78939748
related_name表面作用



加上



核心related_name作用
https://blog.csdn.net/lanyang123456/article/details/68962515
定义表Apple:
class Apple( models.Model):
origin_level = models.ForeignKey(AppleLevel)
new_level = models.ForeignKey(AppleLevel)
就会出现以下问题
monitor.apple: Accessor for field ‘origin_level’ clashes with related field ‘AppleLevel.apple_set’. Add a related_name argument to the definition for ‘origin_level’.
monitor.apple: Accessor for field ‘new_level’ clashes with related field ‘AppleLevel.apple_set’. Add a related_name argument to the definition for ‘new_level’.
解决办法:
使用related_name属性定义名称(related_name是关联对象反向引用描述符)。
具体修改代码如下:
class Apple( models.Model):
origin_level = models.ForeignKey(AppleLevel, related_name='orgin_level_appleset')
new_level = models.ForeignKey(AppleLevel, related_name='new_level_appleset')
[django]Django外键(ForeignKey)操作以及related_name的作用的更多相关文章
- Django学习——Django测试环境搭建、单表查询关键字、神奇的双下划线查询(范围查询)、图书管理系统表设计、外键字段操作、跨表查询理论、基于对象的跨表查询、基于双下划线的跨表查询
Django测试环境搭建 ps: 1.pycharm连接数据库都需要提前下载对应的驱动 2.自带的sqlite3对日期格式数据不敏感 如果后续业务需要使用日期辅助筛选数据那么不推荐使用sqlite3 ...
- 使用admin的步骤、必知必会13条、单表的双下划线、外键的操作、多对多的操作:
MVC M: model 模型 与数据库交互 V: view 视图 HTML C:controller 控制器 流程 和 业务逻辑 MTV M:model ORM T:template 模板 HTML ...
- Django 外键ForeignKey中的on_delete
当你在Django中删除了一个有着外键关联的数据时,比如一个作者和他名下的所有的书的信息,书的外键是作者(一个作者可有好多本书),当你把作者的信息从数据库中删除时,Django提供了一下几个参数来对作 ...
- Django - orm外键操作
1.orm外键操作 创建外键: 备注:ForeignKey两个参数,1个为关联的表名,1个为关联的字段名: 在django2.0后,定义外键和一对一关系的时候需要加on_delete选项,此参数为了避 ...
- Python - Django - ORM 外键操作
models.py: from django.db import models # 出版社 class Publisher(models.Model): id = models.AutoField(p ...
- django 笔记5 外键 ForeignKey
class UsserGroup(models.Model): uid = models.AutoField(primary_key=True) caption = models.CharField( ...
- django ORM 外键详解
Django中的外键: 首先,为了方便理解,我们把使用ForeignKey的字段所在的表定义为从表,把ForeignKey中to参数连接的表称为主表. 外键使用的先决条件: 在mysql数据表中,数据 ...
- Django配合MySQL学习Django模型外键的建立和使用
Django 模型建立外键 在模型中建立外键是很简单的,基本操作如下 class Table(models.Model) column_name = models.ForeignKey(other-T ...
- django 获取外键对应数据的方式
模型 models.py中 from django.db import models class User(models.Model): name = models.CharField() class ...
随机推荐
- Understanding Flash: Blocks, Pages and Program / Erases
https://flashdba.com/2014/06/20/understanding-flash-blocks-pages-and-program-erases/ In the last pos ...
- DB2 Version 10.5 补丁下载
DB2 Version 10.5 for Linux, UNIX, and Windows fix pack summary https://www.ibm.com/support/knowledge ...
- [No0000119]什么是柳比歇夫的时间事件记录法
上图是我过去一年来做的时间事件记录中的某几天的记录文字.从接触到这种方法以来,也就是2009年的7月31日到今天,我已经作了一年多时间的记录.那么什么是时间事件记录?很简单,就像那两幅图片上所展示的, ...
- 【凸包板题】Gym - 101484E E. Double Fence
http://codeforces.com/gym/101484/problem/E 题解 凸包板题 #define _CRT_SECURE_NO_WARNINGS #include<cmath ...
- use of objects can be less efficient than a procedural approach
PHP Advanced and Object-Oriented Programming 3rd Edition As for the technical negatives of OOP, use ...
- [cloud][sdn] LBaaS/neutron / Octavia
清晰/浅显: http://www.cnblogs.com/sammyliu/p/4656176.html IBM:写的一般般,价值不大 https://www.ibm.com/developerwo ...
- [daily][dpdk] 内核模块(网卡驱动)无法卸载
由于程序的异常退出, 内核的引用计数没有被清除(我猜的). 所以驱动不能被卸载掉, 强制也不行. 如下: [root@localhost ~]# insmod /opt/scorpion/KingKo ...
- 《Mysql 日志结构》
推荐一首歌 - <往后余生>王贰浪 发现自己每天就听这么几首歌 一:慢查询日志 - SQL 完成查询并完成对于锁资源的释放之后,msqld 会将符合条件的SQL写入慢日志.因此慢日志可能和 ...
- POJ2431 Expedition 贪心
正解:模拟费用流 解题报告: 先放个传送门鸭,题目大意可以点Descriptions的第二个切换成中文翻译 然后为了方便表述,这里强行改一下题意(问题是一样的只是表述不一样辣,,, 就是说现在在高速公 ...
- websocketd
https://www.cnblogs.com/tinywan/p/6826125.html https://www.jianshu.com/p/63afd0099565