Django model latest()方法 [英] Django model latest() method

查看:123
本文介绍了Django model latest()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题(BTW我认为我之前没有这个问题):

I am having the following issue (BTW I think I had not had this problem the day before):

>>> rule = Rule.objects.get(user=user)
>>> rule.id
1
>>> rule = Rule.objects.get(user=user).latest('id')

AttributeError: 'Rule' object has no attribute 'latest'

为什么我收到错误?

推荐答案

模型管理器的get()函数返回对象本身的一个实例。

The get() function of a model Manager returns an instance of the object itself.

您提到的latest()函数属于QuerySet类。调用.filter(),.all(),.exclude()等都返回一个查询集。

The latest() function you mention belongs to the QuerySet class. Calling .filter(), .all(), .exclude() etc, all return a query set.

你可能要找的是首先过滤特定用户,然后通过'id'获取最新结果:

What you're likely looking for is to first filter for the specific user, then get the latest result by 'id':

rule = Rule.objects.filter(user=user).latest('id')

请参阅这里有关查询模型

这篇关于Django model latest()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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