“ReverseManyToOneDescriptor”对象没有属性“最新”

2024-03-20

我在尝试运行函数时收到此错误。这是我的第一个 django/python 项目,所以我对此没有经验。我已经搜索过此错误,但没有找到类似的内容。

def getpriority(chunks):
    p = 0
    for chunk in chunks:
        a = chunk.result_set.all()
        l = a.latest()
        if pytz.utc.localize(datetime.now()) - l.timestamp > datetime.timedelta(days=3):
            x = getresult(chunk)
            print(x)

我正在尝试为我的块模型分配优先级,以便我可以选择具有最高优先级的块来使用该对象。

我相信我的错误在于调用latest()在“a”上。

当我在 django shell 中运行 Chunk.result_set.latest() 时,出现以下错误:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'ReverseManyToOneDescriptor' object has no attribute 'latest'

在我的结果模型中,我设置了 get_latest_by ,我认为这是运行 .latest() 所必需的:

class Result(models.Model):
    rel_chunk = models.ForeignKey(Chunk, on_delete=models.CASCADE)
    score = models.IntegerField()
    timestamp = models.DateTimeField(auto_now_add=True)

    class Meta:
        get_latest_by = 'timestamp'

我认为错误在于我正在相关对象集上调用最新函数,但如果无法在相关对象集上调用最新函数,那么我如何才能找到最新的相关结果?


它应该是chunk.result_set.latest() not Chunk.result_set.latest()

注意chunk应该是一个实例而不是类模型。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

“ReverseManyToOneDescriptor”对象没有属性“最新” 的相关文章

随机推荐