title: How to create a unique_for_field slug in Django?
body: <p>Django has a <a href="http://docs.djangoproject.com/en/dev/ref/models/fields/#unique-for-date" rel="noreferrer">unique_for_date</a> 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:</p>
  
  <pre><code>class Example(models.Model):
      title = models.CharField()
      slug = models.SlugField(unique_for_date='publish')
      publish = models.DateTimeField()
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>class Example(models.Model):
      title = models.CharField()
      slug = models.SlugField(unique_for='category')
      category = models.ForeignKey(Category)
  </code></pre>
  
  <p>This way I could create the following urls:</p>
  
  <pre><code>/example/category-one/slug
  /example/category-two/slug
  /example/category-two/slug &lt;--Rejected as duplicate
  </code></pre>
  
  <p><strong>My ideas so far:</strong></p>
  
  <ul>
  <li><p><strong>Add a unique index for the slug and categoryid to the table.</strong> This requires code outside of Django. And would the built-in admin handle this correctly when the insert/update fails?</p></li>
  <li><p><strong>Override the save for the model and add my own validation, throwing an error if a duplicate exists.</strong> I know this will work but it doesn't seem very DRY.</p></li>
  <li><p><strong>Create a new slug field inheriting from the base and add the unique_for functionality there.</strong> 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.</p></li>
  </ul>
  
  <p><strong>Any ideas, suggestions or opinions on the best way to do this?</strong></p>