码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-01-25 18:50:38      阅读:470      评论:0      收藏:0      [点我收藏+]

标签:联合   boolean   约束   detail   效率   csdn   default   pre   user   

来自: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 中设置联合约束和联合索引

标签:联合   boolean   约束   detail   效率   csdn   default   pre   user   

原文地址:https://www.cnblogs.com/shengulong/p/10320900.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!