How to create a unique_for_field slug in Django?

后端 未结 1 1422
既然无缘
既然无缘 2021-02-09 17:40

Django has a unique_for_date property you can set when adding a SlugField to your model. This causes the slug to be unique only for the Date of the field you specify:

         


        
1条回答
  •  抹茶落季
    2021-02-09 18:18

    What about unique_together?

    class Example(models.Model):
        title = models.CharField()
        slug = models.SlugField(db_index=False)
        category = models.ForeignKey(Category)
    
        class Meta:
            unique_together = (('slug','category'),)
            # or also working since Django 1.0:
            # unique_together = ('slug','category',)
    

    This creates an index, but it is not outside of Django ;) Or did I miss the point?

    0 讨论(0)
提交回复
热议问题