Home > Software engineering >  how to get only values from query dict
how to get only values from query dict

Time:11-05

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'}]>

i want to get only the values without key and store it in a new list in views.py file like newlist = ['Mar_22' , 'Apr_22', 'May_22','Jun_22']

while i am using

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

it is showing like this

[{'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}]

CodePudding user response:

If you use values_list() [django-docs] with a single field, you can use flat=True to return a QuerySet of single values, I mean:

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)

And this will print just ['Mar_22', 'Apr_22', 'May_22', 'Jun_22'] for you.

  • Related