如何只从查询dict中获取值


if request.method == 'POST': 
product=request.POST.get('product')
upload_month = request.POST.get('upload_month') 
un_month= Planning_quantity_data.objects.values('month').filter(product=product,upload_month=upload_month).distinct()
print(un_month)
<QuerySet [{'month': 'Mar_22'}, {'month': 'Apr_22'}, {'month': 'May_22'}, {'month': 'Jun_22'}]>

我只想获取不带键的值,并将其存储在中的新列表中views.py文件:

like newlist = ['Mar_22' , 'Apr_22', 'May_22','Jun_22']

当我使用时

un_month1=list(un_month.values())
print(un_month1)

它显示的是这样的:

[{'id': 1, 'upload_month': 'Mar_22', 'product': 'MAE675', 'material_code': 'MAE675 (MEMU â OB) RCF', 'order_type': 'Onhand', 'BOM_CODE': '675MEMU', 'month': 'Mar_22', 'quantity': 3, 'po_value': '37/5', 'remarks': 'Qty in Rakes. 3-5 rakes partial qty dispatched', 'empid': None}, {'id': 2, 'upload_month': 'Mar_22', 'product': 'MAE675', 'material_code': 'MAE675 (MEMU â OB) RCF', 'order_type': 'Onhand', 'BOM_CODE': '675MEMU', 'month': 'Apr_22', 'quantity': 3, 'po_value': '37/5', 'remarks': 'Qty in Rakes. 3-5 rakes partial qty dispatched', 'empid': None}, {'id': 3, 'upload_month': 'Mar_22', 'product': 'MAE675', 'material_code': 'MAE675 (MEMU â OB) RCF', 'order_type': 'Onhand', 'BOM_CODE': '675MEMU', 'month': 'May_22', 'quantity': 3, 'po_value': '37/5', 'remarks': 'Qty in Rakes. 3-5 rakes partial qty dispatched', 'empid': None}]

如果您将values_list()[django docs]与单个字段一起使用,则可以使用flat=True返回单个值的QuerySet,我的意思是:

if request.method == 'POST':
product=request.POST.get('product')
upload_month = request.POST.get('upload_month') 
newlist = list(Planning_quantity_data.objects.filter(product=product,upload_month=upload_month).values_list('month', flat=True))
print(newlist)

这将只为您打印['Mar_22', 'Apr_22', 'May_22', 'Jun_22']

相关内容

  • 没有找到相关文章

最新更新