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

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

问题描述

我在尝试运行功能时收到此错误.这是我的第一个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'

在我的Result模型中,我设置了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'

我认为错误在于我在相关对象集上调用最新消息,但是如果不能在相关对象集上调用最新消息,那么我如何找到最新的相关结果?

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"对象没有属性"latest"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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