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:
A sample Need record:
Any tips as to how to create this unique slug? Its composition is:
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!
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.
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.
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.
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.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With