I've been trying to implement hashids in django models. I want to acquire hashid based on model's id
like when model's id=3
then hash encoding should be like this: hashid.encode(id)
. The thing is i can not get id or pk until i save them. What i have in my mind is get the latest objects id
and add 1
on them. But it's not a solution for me. Can anyone help me to figure it out???
django model is:
from hashids import Hashids
hashids = Hashids(salt='thismysalt', min_length=4)
class Article(models.Model):
title = models.CharField(...)
text = models.TextField(...)
hashid = models.CharField(...)
# i know that this is not a good solution. This is meant to be more clear understanding.
def save(self, *args, **kwargs):
super(Article, self).save(*args, **kwargs)
self.hashid = hashids.encode(self.id)
super(Article, self).save(*args, **kwargs)