'ReverseManyToOneDescriptor' object has no attribute 'latest'

為{幸葍}努か 提交于 2020-01-14 18:50:50

问题


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)

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

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

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'

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?


回答1:


It should be chunk.result_set.latest() not Chunk.result_set.latest()

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




回答2:


The error indicates that you're accessing the result_set attribute on the model class. You need to access it on a model instance, in which case it will return a proper manager object instead of a ReverseManyToOneDescriptor:

chunk = Chunk.objects.get(...)
chunk.result_set.latest()

Here, chunk.result_set is a manager that behaves exactly like Result.objects, except it filters the queryset to only include results related to the chunk instance. The manager also has some added methods to manipulate the relation between chunk and the results, such as chunk.results_set.add() and chunk.result_set.remove() to add/remove results from the result set related to this chunk instance.

As this all applies to a specific instance of Chunk, it makes no sense to access the related manager on a class level. That's why you need a Chunk instance to use the related manager.

Since chunk.result_set is a manager instance, it proxies all queryset methods*. There is no reason why you would need chunk.result_set.all().latest() instead of just chunk.result_set.latest().

*) delete() is disabled on managers so you don't accidentally delete all objects. as_manager() is disabled as it makes no sense on a manager.



来源:https://stackoverflow.com/questions/40250430/reversemanytoonedescriptor-object-has-no-attribute-latest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!