CartQuantity匹配查询不存在错误 [英] CartQuantity matching query does not exist ERROR
问题描述
我正在尝试获取查询集中的最新/最新号码:
Hi I'm trying to get the newest/latest number in a query set:
我为此使用此代码行:
CartQuantity.objects.filter(customer=customer).values_list('cquantity', flat=True).get(pk=-1)
这是查询集的外观:
<bound method QuerySet.last of <QuerySet [4, 4, 4, 2, 4, 4, 5, 6, 5, 14, 10, 12]>> # need last number(12)
我尝试了上面的代码,但收到错误消息:
I tried the code above but I get an Error message:
store.models.CartQuantity.DoesNotExist:CartQuantity匹配查询不存在.
store.models.CartQuantity.DoesNotExist: CartQuantity matching query does not exist.
这是我的模型:
class CartQuantity(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, blank=True, null=True)
cquantity = models.IntegerField(default=0, null=True, blank=True)
有人知道如何解决错误或以其他方式获取最新号码(在这种情况下为查询集中的号码12)吗?
Does anyone know how to fix the error or another way of getting the newest number(in this case number 12 in the query set)?
推荐答案
CartQuantity.objects.filter(customer = customer).values_list('cquantity',flat = True)[-1]
或
CartQuantity.objects.filter(customer = customer).values_list('cquantity',flat = True).last()
或
CartQuantity.objects.filter(customer = customer).values_list('cquantity',flat = True).reverse()[0]
这篇关于CartQuantity匹配查询不存在错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!