Django model 中设置联合约束和联合索引

Posted shengulong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django model 中设置联合约束和联合索引相关的知识,希望对你有一定的参考价值。

来自:https://blog.csdn.net/ding_312/article/details/81264910

 

 
class Cart(models.Model):
    user = models.ForeignKey(
        MyUser,
        verbose_name="用户"
    )
    goods = models.ForeignKey(
        Goods,
        verbose_name="商品"
    )
    num = models.IntegerField(
        verbose_name="商品数量"
    )
    is_select = models.BooleanField(
        default=True,
        verbose_name="选中状态"
    )
 
    class Meta:
        # 联合约束   其中goods和user不能重复
        unique_together = ["goods", "user"]
        # 联合索引, 其中"goods"和"user"联合同步查询,提高效率
        index_together = ["user", "goods"]

 

以上是关于Django model 中设置联合约束和联合索引的主要内容,如果未能解决你的问题,请参考以下文章