python - 从查询集中获取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)
解决方案
CartQuantity.objects.filter(customer=customer).values_list('cquantity', flat=True)[-1]
应该给你查询集中的最后一项。
参考: https ://docs.djangoproject.com/en/3.1/ref/models/querysets/#values-list
推荐阅读
- plot - 使用 SGPANEL for SAS 合并类面板
- swift - 如何使用嵌套的 json 创建结构?
- angular - 错误:无法使用回调方法读取未定义的属性(读取“消毒剂”)
- javascript - 如何只从 Redux 加载一次?
- algorithm - 如何解决此动画中当前的不透明度?
- bootstrap-5 - 冲突(换行)
- android - Kotlin:关联特定的文件扩展名以打开没有明确扩展名的“内容”文件
- c# - 如何有效地预先确定我的 .NET Framework 库调用的所有未在 .NET Standard 2.0/2.1 中真正实现的方法?
- r - 您如何在 R 中读取原始 Excel 向量
- python - float dtype比较中系列的模棱两可的真值