如何在 Django 中创建 unique_for_field slug?

发布于 2024-08-19 23:23:20 字数 1179 浏览 6 评论 0原文

Django 有一个可以设置的 unique_for_date 属性将 SlugField 添加到模型时。这会导致 slug 仅对于您指定的字段的日期是唯一的:

class Example(models.Model):
    title = models.CharField()
    slug = models.SlugField(unique_for_date='publish')
    publish = models.DateTimeField()

对于像外键这样的非日期时间字段实现相同类型的功能的最佳方法是什么?理想情况下,我想做这样的事情:

class Example(models.Model):
    title = models.CharField()
    slug = models.SlugField(unique_for='category')
    category = models.ForeignKey(Category)

这样我可以创建以下网址:

/example/category-one/slug
/example/category-two/slug
/example/category-two/slug <--Rejected as duplicate

到目前为止我的想法:

  • 将 slug 和 Categoryid 的唯一索引添加到表中。 这需要 Django 之外的代码。当插入/更新失败时,内置管理会正确处理这个问题吗?

  • 覆盖模型的保存并添加我自己的验证,如果存在重复则抛出错误。我知道这会起作用,但看起来不太干燥。

  • 创建一个从基础继承的新 slug 字段,并在其中添加 unique_for 功能。 这似乎是最好的方法,但我查看了核心的 unique_for_date 代码,扩展起来似乎不太直观

关于实现此目的的最佳方法有什么想法、建议或意见吗?

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:

class Example(models.Model):
    title = models.CharField()
    slug = models.SlugField(unique_for_date='publish')
    publish = models.DateTimeField()

What would be the best way to achieve the same kind of functionality for a non-DateTime field like a ForeignKey? Ideally, I want to do something like this:

class Example(models.Model):
    title = models.CharField()
    slug = models.SlugField(unique_for='category')
    category = models.ForeignKey(Category)

This way I could create the following urls:

/example/category-one/slug
/example/category-two/slug
/example/category-two/slug <--Rejected as duplicate

My ideas so far:

  • Add a unique index for the slug and categoryid to the table. This requires code outside of Django. And would the built-in admin handle this correctly when the insert/update fails?

  • Override the save for the model and add my own validation, throwing an error if a duplicate exists. I know this will work but it doesn't seem very DRY.

  • Create a new slug field inheriting from the base and add the unique_for functionality there. This seems like the best way but I looked through the core's unique_for_date code and it didn't seem very intuitive to extend it.

Any ideas, suggestions or opinions on the best way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赏烟花じ飞满天 2024-08-26 23:23:20

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',)

这会创建一个索引,但它不在 Django 之外;) 或者我错过了重点吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文