我一直在尝试在 django 模型中实现hashids 。我想根据模型获取 hashid,id
当模型的id=3
哈希编码应该是这样的时:hashid.encode(id)
. 问题是在我保存它们之前我无法获得 id 或 pk。我的想法是获取最新的对象id
并添加1
它们。但这对我来说不是解决方案。谁能帮我弄清楚???
django模型是:
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)