python django model联合主键
2018-03-29 10:19
411 查看
https://docs.djangoproject.com/en/2.0/topics/db/models/#meta-options
Give your model metadata by using an inner
https://docs.djangoproject.com/en/2.0/ref/models/options/#unique-together
今天,在家试试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做外键,也做联合主键,如下:
Meta
options¶
Give your model metadata by using an inner class Meta, like so:
from django.db import models class Ox(models.Model): horn_length = models.IntegerField() class Meta: ordering = ["horn_length"] verbose_name_plural = "oxen"Model metadata is “anything that’s not a field”, such as ordering options(
ordering), database table name (
db_table), orhuman-readable singular and plural names (
verbose_nameand
verbose_name_plural). None are required, and adding
classMetato a model is completely optional.A complete list of all possible
Metaoptions can be found in the modeloption reference.
https://docs.djangoproject.com/en/2.0/ref/models/options/#unique-together
unique_together
¶
Options.
unique_together¶Sets of field names that, taken together, must be unique:
unique_together = (("driver", "restaurant"),)This is a tuple of tuples that must be unique when considered together.It’s used in the Django admin and is enforced at the database level (i.e., theappropriate
UNIQUEstatements are included in the
CREATE TABLEstatement).For convenience, unique_together can be a single tuple when dealing with a singleset of fields:
unique_together = ("driver", "restaurant")A
ManyToManyFieldcannot be included inunique_together. (It’s not clear what that would even mean!) If youneed to validate uniqueness related to a
ManyToManyField, try using a signal oran explicit
throughmodel.The
ValidationErrorraised during model validation when the constraintis violated has the
unique_togethererror code.
今天,在家试试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 怎么使用 UUID 作为主键?
- 【Python】 Django 怎么实现 联合主键?
- Python3之外部文件调用Django程序操作model等文件实现
- Python开发【Django】:Model操作(一)
- django model 主键自增问题
- python django model类型总结
- python-django 模型model字段类型说明
- django model 主键自增问题
- Django model中设置多个字段联合唯一约束
- 使用pycharm手动搭建python语言django开发环境(四) django中buffer类型与str类型的联合使用
- PYTHON--DJANGO--MODEL--多表查询
- Python Shell 解释器下使用Django Model
- python-django 模型model字段类型说明
- Python Django model之字段
- python Django 1.7 中文入门 (官网) 04 设计model
- 单独的 python 脚本文件使用 django 自带的 model
- 外部python脚本调用django model
- python3-开发进阶Django-form组件中model form组件
- Python工程师详细讲解Django之model