猿问

如何计算 django 中 id 低于当前 id 的对象

class Shoe(models.Model):
     title = models.Charfield(max_length=120)

我正在使用的查询是:

old_shoes = Shoe.objects.all().count()

我想买 id 低于当前 id 的鞋子,这样行吗

old_shoes = Shoe.objects.all(id>shoe.id).count()


呼啦一阵风
浏览 121回答 2
2回答

回首忆惘然

您应该使用filter, 因为all将返回模型 Shoe 的所有对象。此外,您需要使用__lt而不是<过滤 QuerySet。请参阅 Django 文档所以使用old_shoes = Shoe.objects.filter(id__lt = shoe.id).count()

蓝山帝景

为了过滤低于给定 idShoe的 s ,您可以使用查找 [Django-doc]:id__ltold_shoes&nbsp;=&nbsp;Shoe.objects.filter(id__lt=shoe.id).count()
随时随地看视频慕课网APP

相关分类

Python
我要回答