Creating Partial Indexes with Django 1.7

前端 未结 2 743
小鲜肉
小鲜肉 2020-12-05 19:08

The documentation for Django 1.7 mentions RunSQL classes can be used to create partial indexes on your tables. I have a table where I want the combination of

2条回答
  •  执念已碎
    2020-12-05 19:48

    You could just provide an unique_together like so:

    class Post(models.Model):
        title = models.CharField(max_length=200)
        blog = models.ForeignKey(Blog)
        category = models.ForeignKey(Category, null=True, blank=True)
    
    class Meta:
        unique_together = ("title", "blog", "category")
    

    NULLs for category will work how you want in that if not set then title/blog has to be unique.

    https://docs.djangoproject.com/en/1.8/ref/models/options/#unique-together

提交回复
热议问题