Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.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 如何访问model对象中Django模型的id?_Python_Django - Fatal编程技术网

Python 如何访问model对象中Django模型的id?

Python 如何访问model对象中Django模型的id?,python,django,Python,Django,我有一个简单的模型,我想在其中包含模型id的哈希: class pick(models.Model): id_hash = models.TextField(default=hashlib.md5(id).hexdigest()) symbol = models.CharField(max_length=5) buy_datetime = models.DateTimeField() buy_price = models.DecimalField(max_digi

我有一个简单的模型,我想在其中包含模型id的哈希:

class pick(models.Model):
    id_hash = models.TextField(default=hashlib.md5(id).hexdigest())
    symbol = models.CharField(max_length=5)
    buy_datetime = models.DateTimeField()
    buy_price = models.DecimalField(max_digits=8, decimal_places=2)
    sell_price = models.DecimalField(max_digits=8, decimal_places=2, null=True, blank=True)
    buy_quantity = models.IntegerField()
    current_price = models.DecimalField(max_digits=8, decimal_places=2)
    def hash(self):
        return hashlib.md5(str(self.id)).hexdigest()
    def __str__(self):
        return "{} {} {} {} {} {}".format(self.symbol,self.buy_datetime,self.buy_price,self.sell_price,self.buy_quantity, self.current_price)

当然,对于id_散列,我没有访问model类内部的
id
。有什么方法可以尝试达到相同的效果吗?

您可以覆盖
保存
方法,因为
id
是从数据库自动生成的,您必须至少保存两次-

class pick(models.Model):
    id_hash = models.TextField(default="")
    symbol = models.CharField(max_length=5)
    buy_datetime = models.DateTimeField()
    buy_price = models.DecimalField(max_digits=8, decimal_places=2)
    sell_price = models.DecimalField(max_digits=8, decimal_places=2, null=True, blank=True)
    buy_quantity = models.IntegerField()
    current_price = models.DecimalField(max_digits=8, decimal_places=2)
    def hash(self):
        return hashlib.md5(str(self.id)).hexdigest()
    def __str__(self):
        return "{} {} {} {} {} {}".format(self.symbol,self.buy_datetime,self.buy_price,self.sell_price,self.buy_quantity, self.current_price)

    def save(self, *args, **kwargs):
        with transaction.atomic():
            super().save(*args, **kwargs)
            # for python < 3.0 super(pick, self).save(*args, **kwargs) 
            self.id_hash = self.hash()
            super().save(*args, **kwargs)
            # for python < 3.0 super(pick, self).save(*args, **kwargs) 

您希望覆盖模型的create方法,以便在初始保存之后写入id_散列。我假设您不想多次写入id\u散列。比如说,可能是

class PickManager(models.Manager):
    def create(**kwargs):
        instance = super(PickManager, self).create(**kwargs)
        instance.id_hash = hashlib.md5(instance.id).hexdigest()
        instance.save()

class Pick(models.Model):
    id_hash = models.TextField(blank=True)
    ...
    objects = PickManager()

此外,您应该大写您的型号(Pick),并可能使用unicode而不是str

我不完全理解这个问题。你是想说将自动计算
id\u散列
吗?我想将id的散列保存在数据库表中id的旁边,你看,我想搜索id\u散列的记录,我不想通过将它们全部放在ram中来搜索所有拾取对象,相反,我想使用sql搜索,这只能通过将哈希放入数据库中来实现谢谢,但是对于第二个选项,我是否能够以与搜索id_哈希相同的速度和使用相同数量的ram来搜索id_哈希?不,第二个选项不适用于sql,因为
id\u hash
是一个属性而不是db列,所以检查将在ram中完成。所以我无法使用第二个选项按id\u hash进行搜索?或者我只能用一个标准的python循环来完成?是的。。。您可以在python循环中搜索。但不是数据库中的SQL,它说super至少需要1个参数?
class PickManager(models.Manager):
    def create(**kwargs):
        instance = super(PickManager, self).create(**kwargs)
        instance.id_hash = hashlib.md5(instance.id).hexdigest()
        instance.save()

class Pick(models.Model):
    id_hash = models.TextField(blank=True)
    ...
    objects = PickManager()