0

My question is, is there a way to generate Django slugs on the fly WITHOUT storing them in the DB?

I have been tinkering with get_absolute_url but no luck yet.

def get_absolute_url(self):
        randstr = hashlib.sha256(str(random.getrandbits(256)).encode('utf-8')).hexdigest()
        return reverse(args=[randstr])

Yea I know, why not use uuid, right? Not the point, the question is how to generate slugs on the fly WITHOUT storing them in the DB? I would my urls to change that's the point.

quba
  • 123
  • 9
  • The problem with not storing them in the database, is that you later need to *retrieve* the object with that slug, and if these are not stored in the database (with an index), it will result in fetching *all* records, and iterating over these, which will take considerable time. – Willem Van Onsem Oct 05 '20 at 21:49
  • If you're just trying to create a slug from a value, you can use the slugify function: https://docs.djangoproject.com/en/3.1/ref/utils/#django.utils.text.slugify – Ben Oct 05 '20 at 22:25
  • @ Willem Van Onsem, there's the pk to retrieve objects, I feel like this should be doable or allowed, if not currently allowed. I had slugs in DB but never need them, with current version of Django, slugs are nice to have to show there's security on the site but not necessary. – quba Oct 05 '20 at 22:27

0 Answers0