Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 588995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:22:38+00:00 2026-05-13T15:22:38+00:00

Django has a unique_for_date property you can set when adding a SlugField to your

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:22:38+00:00Added an answer on May 13, 2026 at 3:22 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a unique slug in Django so that I can
Given that django-nonrel has got JOINs working, does this mean we have M2M fields
My friend says that Django only has 1 thread or something? And I have
I've defined a class called Country that has a unique name field. class Country(models.Model):
I would like to have a model in Django that has multiple pictures associated
Django has a number of open source projects that tackle one of the framework's
Django has various numeric fields available for use in models, e.g. DecimalField and PositiveIntegerField
My Django app has a Person table, which contains the following text in a
My django application has become painfully slow on the production. Probably it is due
I am making a custom form object in Django which has an overrided __init__

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.