python django model联合主键的例子
作者:超越自己看到的 时间:2023-10-09 19:18:17
今天,在家试试django的model的设置,如何设置的联合主键,我经过查资料和实践,把结果记录如下:
例如:
class user(Model):
id=AutoField(primary_key=True)
name = CharField(max_length=30)
age =IntegerField()
class role(Model):
id=AutoField(primary_key=True)
name=CharField(max_length=10)
这是两个model有一个roleUser的model来描述use与role的关系,需要user的id与role的id做外键,也做联合主键,如下:
class roleUser(Model):
userId=ForeignKey(user)
roleId=ForeignKey(role)
class Meta:
unique_together=("userId","roleId")
其中:
class Meta:
unique_together=("userId","roleId")
就是建立联合主键。
来源:https://blog.csdn.net/lilovfly/article/details/77127111
标签:python,django,model,主键
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Oracle 10g的DBA无法登录解决方案
2009-05-24 19:38:00
INSERT INTO .. ON DUPLICATE KEY更新多行记录
2024-01-21 23:27:49
从品牌网站看交互设计
2009-08-18 12:39:00
![](https://img.aspxhome.com/file/UploadPic/20098/18/1-31s.jpg)
python 用matplotlib绘制折线图详情
2022-03-23 16:38:19
![](https://img.aspxhome.com/file/2023/3/92003_0s.png)
Vite创建项目的实现步骤
2024-04-30 10:38:16
![](https://img.aspxhome.com/file/2023/0/130130_0s.png)
OpenCV实现图像滤波之双边滤波
2022-05-14 07:35:30
![](https://img.aspxhome.com/file/2023/6/82536_0s.jpg)
用python生成与调用cntk模型代码演示方法
2023-04-27 10:55:39
![](https://img.aspxhome.com/file/2023/1/115871_0s.jpg)
python使用 request 发送表单数据操作示例
2022-08-06 07:31:07
![](https://img.aspxhome.com/file/2023/0/131250_0s.png)
Python实现一个简单的递归下降分析器
2022-10-05 16:23:13
分布式系统CAP定理中的P原理解析
2022-09-04 11:16:15
Python实现笑脸检测+人脸口罩检测功能
2022-06-24 04:01:49
![](https://img.aspxhome.com/file/2023/4/87474_0s.png)
ASP利用XMLHTTP实现表单提交以及cookies的发送的代码
2011-04-15 10:37:00
全兼容可高亮二级缓冲折叠菜单
2010-06-03 16:53:00
![](https://img.aspxhome.com/file/UploadPic/20106/3/1-86s.gif)
段正淳的css笔记(1)分类之间的横竖线
2007-11-01 21:47:00
![](https://img.aspxhome.com/file/UploadPic/200711/1/2007111215159211s.jpg)
ExtJs事件机制基本代码模型和流程解析
2024-05-03 15:59:14
重命名SQLServer数据库的方法
2012-07-11 15:39:37
使用递归算法结合数据库解析成Java树形结构的代码解析
2024-01-18 00:11:33
超越MYSQL,ACCESS复合承载
2008-12-09 13:31:00
vue生成随机验证码的示例代码
2023-07-02 16:57:09
![](https://img.aspxhome.com/file/2023/3/139843_0s.png)
淘宝网提供的国内NPM镜像简介和使用方法
2024-04-17 10:10:16