Django选择最大ID [英] Django select max id

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

问题描述

给定具有自动设置为 id的标准模型(称为图片),如何获得最大ID?

given a standard model (called Image) with an autoset 'id', how do I get the max id?

到目前为止,我已经尝试过:

So far I've tried:

max_id = Image.objects.all().aggregate(Max('id'))

但是我收到'id__max'键错误。

but I get a 'id__max' Key error.

尝试

max_id = Image.objects.order_by('id')[0].id

给出了一个参数2,map()必须支持迭代异常

gives a 'argument 2 to map() must support iteration' exception

有帮助吗?

推荐答案

只需按反向ID排序,然后取前一个即可。

Just order by reverse id, and take the top one.

Image.objects.all().order_by("-id")[0]

这篇关于Django选择最大ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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