首页 > 解决方案 > 从查询集中获取cleaned_data

问题描述

嗨我怎样才能从查询集中获得清理的数据?我可以在查询集上使用 .split() 吗?

前任。

CartQuantity.objects.filter(customer=customer).values_list('cquantity', flat=True)

上面的代码打印了这个:

<bound method QuerySet.last of <QuerySet [4, 4, 4, 2, 4, 4, 5, 6, 5, 14, 10, 12]>>  # need last number (12)

但是我只需要数字 12(添加到模型中的最新/最新数字)我尝试使用.cleaned_data来仅获取数字(没有 <QuerySet 等)和.split

我需要数字 12 进行 while 循环。

编辑:保存数字的模型:

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)

标签: pythondjango

解决方案


CartQuantity.objects.filter(customer=customer).values_list('cquantity', flat=True)[-1]

应该给你查询集中的最后一项。

参考: https ://docs.djangoproject.com/en/3.1/ref/models/querysets/#values-list


推荐阅读