python Django 反向访问器的外键冲突解决
作者:jz 时间:2022-05-19 23:30:40
我有两个继承一个基类的Django模型:
- Request
- Inquiry
- Analysis
请求有两个外键到内置用户模型。
create_user = models.ForeignKey(User, related_name='requests_created')
assign_user = models.ForeignKey(User, related_name='requests_assigned')
由于某种原因,我得到的错误
“Analysis.assign_user”的反向访问器与“Inquiry.assign_user”的反向访问器冲突。
我读过的所有内容说,设置related_name应该防止冲突,但我仍然得到相同的错误。任何人都能想到为什么会发生这种情况?谢谢!
related_name将确保字段不会彼此冲突,但您有两个模型,每个模型都有这两个字段。你需要把每个具体模型的名字,你可以做一些特殊的 string substitution:
create_user = models.ForeignKey(User, related_name='%(class)s_requests_created')
补充知识:django related_name禁用反向映射
官方文档处理办法:
直接将related_name赋值为加号或以加号结尾的字符串,即可实现禁用反向映射
来源:https://stackoverflow.com/questions/22538563/django-reverse-accessors-for-foreign-keys-clashing
标签:Django,反向,访问器,外键
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用LibTorch进行C++调用pytorch模型方式
2021-09-17 18:56:13
![](https://img.aspxhome.com/file/2023/8/85028_0s.png)
Pymysql实现往表中插入数据过程解析
2022-03-24 10:31:43
![](https://img.aspxhome.com/file/2023/3/66803_0s.png)
远程连接阿里云SqlServer 2012 数据库服务器的图文教程
2024-01-16 06:16:48
![](https://img.aspxhome.com/file/2023/9/99989_0s.png)
使用python实现哈希表、字典、集合操作
2023-11-24 21:09:13
![](https://img.aspxhome.com/file/2023/8/117268_0s.png)
python动态文本进度条的实例代码
2021-11-15 02:11:50
![](https://img.aspxhome.com/file/2023/0/89070_0s.gif)
python 将md5转为16字节的方法
2023-09-17 18:46:37
PHP设计模式中的命令模式
2023-05-27 21:13:43
Python 离线工作环境搭建的方法步骤
2023-08-30 01:59:21
使用Python编写类UNIX系统的命令行工具的教程
2023-08-24 05:03:02
Python PyAutoGUI模块控制鼠标和键盘实现自动化任务详解
2021-07-16 07:40:40
![](https://img.aspxhome.com/file/2023/9/107619_0s.png)
浅析Go中序列化与反序列化的基本使用
2024-02-13 00:40:46
![](https://img.aspxhome.com/file/2023/1/108081_0s.png)
Python适配器模式代码实现解析
2023-04-15 12:58:14
Python 线程池模块之多线程操作代码
2022-06-14 06:01:47
Python曲线拟合详解
2023-12-29 05:54:50
![](https://img.aspxhome.com/file/2023/7/105027_0s.jpg)
一文让你彻底搞懂Python中__str__和__repr__
2021-10-30 20:09:56
8种用Python实现线性回归的方法对比详解
2022-03-28 08:41:15
![](https://img.aspxhome.com/file/2023/9/120359_0s.jpg)
如何利用SQL Server 2005中的模板参数
2009-01-23 15:02:00
Python面向对象程序设计之类的定义与继承简单示例
2022-03-24 03:00:16
关于SQL Server数据库中转储设备分析
2009-01-21 14:55:00
MS SQL2000 数据库自动备份方法
2010-07-22 19:52:00