python django model联合主键的示例分析

2023-05-21,,

这篇文章给大家分享的是有关python 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")

就是建立联合主键。

感谢各位的阅读!关于“python django model联合主键的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

《python django model联合主键的示例分析.doc》

下载本文的Word格式文档,以方便收藏与打印。