“ReverseManyToOneDescriptor"对象没有“最新"属性 [英] 'ReverseManyToOneDescriptor' object has no attribute 'latest'

查看:46
本文介绍了“ReverseManyToOneDescriptor"对象没有“最新"属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have received this error when trying to run a function. This is my first django/python project so I am not experienced in this. I have searched for this error but not found anything similar.

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)

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

I am trying to assign priorities to my Chunk model so I can select the Chunk with the highest priority to use the object.

我相信我的错误是在'a'上调用latest().

I believe that my error is in calling latest() on 'a'.

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

When I run Chunk.result_set.latest() in a django shell I get the following error:

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

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

In my Result model, I have set get_latest_by which I believe is required to run .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'

我认为错误在于我在相关对象集上调用了 latest 但如果无法在相关对象集上调用它,那么我如何找到最新的相关结果?

I believe that the error lies in the fact that I'm calling latest on a related object set but if this can't be called on a related object set then how can I find the latest related Result?

推荐答案

应该是 chunk.result_set.latest() 而不是 Chunk.result_set.latest()

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

note that chunk should be a instance not a class model.

这篇关于“ReverseManyToOneDescriptor"对象没有“最新"属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆