关于django中model的多字段联合唯一索引

有时候一个表多字段,要实现字段之间两两联合唯一怎么办了?下面就是办法!!!

class UserOrderHouse(models.Model):
    """
    客户预约表
    """
    broker_id = models.ForeignKey(to="Broker", verbose_name="经纪人", null=True, blank=True)
    client_id = models.ForeignKey(to="Client", verbose_name="游客", null=True, blank=True)
    house = models.ForeignKey(to="house.House", verbose_name="楼盘预约", null=True, blank=True)
    rend_house = models.ForeignKey(to="old_house.RentHouse", verbose_name="出租预约", null=True, blank=True)
    sell_house = models.ForeignKey(to="old_house.SellHouse", verbose_name="求租预约", null=True, blank=True)
    buy_house = models.ForeignKey(to="old_house.BuyHouse", verbose_name="求售预约", null=True, blank=True)
    time = models.DateTimeField(auto_now=True, verbose_name="预约时间")

    class Meta:
        unique_together = (
            ('broker_id', 'house'),
            ('broker_id', 'rend_house'),
            ('broker_id', 'sell_house'),
            ('broker_id', 'buy_house'),
            ('client_id', 'house'),
            ('client_id', 'rend_house'),
            ('client_id', 'sell_house'),
            ('client_id', 'buy_house'),
        )
        verbose_name = "用户预约"
        verbose_name_plural = verbose_name

你可能感兴趣的:(Python自学)