Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在django中实现hashid_Python_Django_Hashids - Fatal编程技术网

Python 在django中实现hashid

Python 在django中实现hashid,python,django,hashids,Python,Django,Hashids,我一直在尝试在django模型中实现。我想根据模型的id获取hashid,比如当模型的id=3时,哈希编码应该是这样的:hashid.encode(id)。问题是,在我保存它们之前,我无法获取id或pk。我脑子里想的是获取最新的对象id,并在其上添加1。但这不是我的解决办法。有人能帮我弄清楚吗 django模型是: from hashids import Hashids hashids = Hashids(salt='thismysalt', min_length=4) class Art

我一直在尝试在django模型中实现。我想根据模型的
id
获取hashid,比如当模型的
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) 

我只会在没有ID的情况下告诉它保存,所以它不会每次都运行代码。您可以使用TimestamedModel继承来实现这一点,这实际上非常适合在任何项目中使用

from hashids import Hashids


hashids = Hashids(salt='thismysalt', min_length=4)


class TimeStampedModel(models.Model):
    """ Provides timestamps wherever it is subclassed """
    created = models.DateTimeField(editable=False)
    modified = models.DateTimeField()

    def save(self, *args, **kwargs):  # On `save()`, update timestamps
        if not self.created:
            self.created = timezone.now()
        self.modified = timezone.now()
        return super().save(*args, **kwargs)

    class Meta:
        abstract = True  


class Article(TimeStampedModel):
    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)
        if self.created == self.modified:  # Only run the first time instance is created (where created & modified will be the same)
            self.hashid = hashids.encode(self.id)
            self.save(update_fields=['hashid']) 

我认为HashID总是为特定id返回相同的值。因此,您可以在显示它之前计算它(使用模板标记)

但如果仍要保存,一种方法是在视图中保存hashid字段,如下所示:

instance = Article()
instance.title = 'whatever...'
instance.text = 'whatever...'
instance.save()

hashids = Hashids()    
instance.hashid = hashids.encode(instance.id)
instance.save()

(我不知道这是否是最好的方法,但它对我很有效!)

这个方法不是在保存后创建hashid,你是通过命令行创建文章的吗?该方法在创建模型实例时不运行“save()”方法。不,我是通过管理面板创建文章的。这是因为修改日期和创建日期不相等吗???另外,当对象保存时调用Python对象时,它给了我超过最大递归深度的值,这是从视图中获得的一种很好的方法