Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a unique slug in Django

I am trying to create a unique slug in Django so that I can access a post via a url like this: http://www.example.com/buy-a-new-bike_Boston-MA-02111_2

The relevant models:

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 

A sample ZipCode record:

  • zipcode = "02111"
  • city = "Boston"
  • statecode = "MA"

A sample Need record:

  • title = "buy a new bike"
  • us_zip = "02111"
  • slug = "buy-a-new-bike_Boston-MA-02111_2" (desired)

Any tips as to how to create this unique slug? Its composition is:

  • Need.title + "_" + Need.get_city() + "_" + an optional incrementing integer to make it unique. All spaces should be replaced with "-".

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.

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?

Thank you!

UPDATE

I'm already using django-extensions for its UUIDField, so it would be nice if it could also be usable for its AutoSlugField!

like image 496
mitchf Avatar asked Sep 28 '10 19:09

mitchf


People also ask

How do you make a unique slug?

you can easily create unique slug in laravel 6, laravel 7, laravel 8 and laravel 9 version. Sometime we need to create slug from title with your application. but you also need to create unique slug on that table. might be user will enter same title then it should automatically generate unique slug.

How do I add a slug in Django?

Adding Slugify to our project: Now we need to find a way to convert the title into a slug automatically. We want this script to be triggered every time a new instance of Post model is created. For this purpose, we will use signals. Note: Add new file util.py in the same directory where settings.py file is saved.

Should slug be unique?

Each slug on your web pages needs to be unique, and they provide readers and search engines alike with information about the contents of a web page or post.

What is a slug field in Django?

A slug is a short label for something, containing only letters, numbers, underscores or hyphens. They're generally used in URLs. ( as in Django docs) A slug field in Django is used to store and generate valid URLs for your dynamically created web pages.


2 Answers

I use this snippet for generating unique slug and my typical save method look like below

slug will be Django SlugField with blank=True but enforce slug in save method.

typical save method for Need model might look below

def save(self, **kwargs):     slug_str = "%s %s" % (self.title, self.us_zip)      unique_slugify(self, slug_str)      super(Need, self).save(**kwargs) 

and this will generate slug like buy-a-new-bike_Boston-MA-02111 , buy-a-new-bike_Boston-MA-02111-1 and so on. Output might be little different but you can always go through snippet and customize to your needs.

like image 192
Srikanth Chundi Avatar answered Sep 23 '22 06:09

Srikanth Chundi


My little code:

def save(self, *args, **kwargs):     strtime = "".join(str(time()).split("."))     string = "%s-%s" % (strtime[7:], self.title)     self.slug = slugify(string)     super(Need, self).save() 
like image 36
Nips Avatar answered Sep 20 '22 06:09

Nips