title: How to create a unique slug in Django
body: <p>I am trying to create a unique slug in Django so that I can access a post via a url like this:
  <a href="http://www.example.com/buy-a-new-bike_Boston-MA-02111_2" rel="noreferrer">http://www.example.com/buy-a-new-bike_Boston-MA-02111_2</a></p>
  
  <p>The relevant models:</p>
  
  <pre><code>class ZipCode(models.Model):
      zipcode = models.CharField(max_length=5)
      city = models.CharField(max_length=64)
      statecode = models.CharField(max_length=32)
  
  class Need(models.Model):
      title = models.CharField(max_length=50)
      us_zip = models.CharField(max_length=5)
      slug = ?????
  
      def get_city():
          zip = ZipCode.objects.get(zipcode=self.us_zip)
          city = "%s, %s %s" % (zip.city, zip.statecode, zip.zipcode)
          return city
  </code></pre>
  
  <p>A sample ZipCode record:</p>
  
  <ul>
  <li>zipcode = "02111"</li>
  <li>city = "Boston"</li>
  <li>statecode = "MA"</li>
  </ul>
  
  <p>A sample Need record:</p>
  
  <ul>
  <li>title = "buy a new bike"</li>
  <li>us_zip = "02111"</li>
  <li>slug = "buy-a-new-bike_Boston-MA-02111_2" (desired)</li>
  </ul>
  
  <p>Any tips as to how to create this unique slug? Its composition is:</p>
  
  <ul>
  <li>Need.title + "_" + Need.get_city() + "_" + an optional incrementing integer to make it unique. All spaces should be replaced with "-".</li>
  </ul>
  
  <p>NOTE: My desired slug above assumes that the slug "buy-a-new-bike_Boston-MA-02111" already exists, which is what it has the "_2" appended to it to make it unique.</p>
  
  <p>I've tried django-extensions, but it seems that it can only take a field or tuple of fields to construct the unique slug. I need to pass in the get_city() function as well as the "_" connector between the title and city. Anyone solved this and willing to share?</p>
  
  <p>Thank you!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I'm already using django-extensions for its UUIDField, so it would be nice if it could also be usable for its AutoSlugField!</p>