How to append values to python list instead of replacing the values with latest value
17,467
jobs = []
values = Record.objects.filter(record_id__in=[1,2,3], is_main_record=True, status='open', set__in=['sasi', 'kuttu','vava'])
if values.exists():
all_item = ['sasi', 'kuttu','vava']
for mask in all_item:
for x in values:
data = {'item' : x.item, 'device': x.device, 'log': x.log}
jobs.append(data)
print jobs # <--- print here, then you see the whole list
your code is fine, you were just printing inside a loop step which gives you just the momentum value and not the whole list
I have updated the queries, which makes less db queries.
Author by
BLACK PANTHER
Updated on June 07, 2022Comments
-
BLACK PANTHER almost 2 years
Hi the following is my python/django code:
values = Record.objects.filter(record_id__in=[1,2,3], is_main_record=True) if values .filter(status='open'): all_item = ['sasi', 'kuttu','vava'] for item in items: values1 = values.filter(set=mask, status='open') print values1
The above code will give me objects in each item. Now I want to create a dictionary so that I can create a data structure as below:
jobs = [ {'item': 'A', 'device': 'deviceA', 'log': 'logA'}, {'item': 'A', 'device': 'device1', 'log': 'ptrf1'}, {'item': 'B', 'device': 'deviceb', 'log': 'ptrfb'}, {'item': 'C', 'device': 'devicec', 'log': 'ptrfc'}, ]
For that I have added like:
jobs = [] values = Record.objects.filter(record_id__in=[1,2,3], is_main_record=True) if values .filter(status='open'): all_item = ['sasi', 'kuttu','vava'] for mask in all_item: values1 = values.filter(set=mask, status='open') for x in values1: data = {'item' : x.item, 'device':x.device, 'log':x.log} jobs.append(data) print jobs
But jobs always giving me the latest item, instead of the data structure i mentioned above (List of Each items). How can I resolve this and achieve the data structure I mentioned? Sorry new to Python and coding. Please forgive if i asked any stupid question. Thanks in advance guys.