1

How can I make a dictionary efficiently from a queryset.

I have days and each day has times associated with it like

Day1 - Time1
Day1 - Time2
Day1 - Time3

Day2 - Time1

I would like to make a list of dictionary like [{"day":'Day1', "time":'Time1, Time2, Time3'}, ...]

This join

listof = ",".join(str([ab.day.day, str(ab.time)]) for ab in vqs.clinicDoctor_dayShift.all().order_by('day__id'))

gives me string like

['Sunday', '15:01:00'],['Sunday', '16:02:00'],['Monday', '09:01:00'],['Monday', '15:01:00'],['Monday', '16:02:00'],['Tuesday', '09:01:00'],['Tuesday', '16:02:00']

I would like to have a list like this

[{'day':'Sunday', 'time':'15:01:00, 16:02:00, 09:01:00'},{'day':'Monday', 'time':'15:01:00, 16:02:00'},{'day':'Tuesday', 'time':'09:01:00, 16:02:00'}]

Can anyone please help me with this.

Thank you.

Hamid Khan
  • 115
  • 2
  • 12

2 Answers2

1

You can use values():

vqs.clinicDoctor_dayShift.values('day__day', 'time').order_by('day__id')

For grouping try groupby:

from itertools import groupby

raw_result = vqs.clinicDoctor_dayShift.values('day__day', 'time').order_by('day__id')
sorted_result = sorted(result, key=lambda x: x.get('day__day'))

result = []
for k, g in groupby(sorted_result, key=lambda x: x.get('day__day')):
    result.append({'day':k, 'time': ','.join(str(item['time']) for item in g)})
neverwalkaloner
  • 46,181
  • 7
  • 92
  • 100
  • It gives me this [{'time': datetime.time(15, 1), 'day__day': 'Sunday'}, {'time': datetime.time(16, 2), 'day__day': 'Sunday'}, {'time': datetime.time(9, 1), 'day__day': 'Monday'}, {'time': datetime.time(15, 1), 'day__day': 'Monday'}, {'time': datetime.time(16, 2), 'day__day': 'Monday'}, {'time': datetime.time(9, 1), 'day__day': 'Tuesday'}, {'time': datetime.time(16, 2), 'day__day': 'Tuesday'}] It not group how I mentioned in the question. Thank you – Hamid Khan Feb 09 '18 at 12:44
  • @HamidKhan try updated answer. I not tested it, but I hope the main idea will help you. – neverwalkaloner Feb 09 '18 at 12:55
  • It seems to do the grouping. One question, is it efficient if this is run in for loop that may have to run 1000+ times? I have a querySet that will have many records which all need this sort of grouping. Thank you – Hamid Khan Feb 09 '18 at 15:38
  • @HamidKhan well it's hard to say without test, but sure this will slow down your code, probably you'd better change data structure? Do you really need this kind of grouping? – neverwalkaloner Feb 09 '18 at 15:58
  • Yes. It seems i need that kind of grouping. I need to show data like I have this pic https://i.stack.imgur.com/pceXe.png – Hamid Khan Feb 09 '18 at 16:19
  • @HamidKhan probably you could fetch data with raw query like this: https://stackoverflow.com/questions/111341/combine-multiple-results-in-a-subquery-into-a-single-comma-separated-value Not sure if this faster or not. – neverwalkaloner Feb 09 '18 at 16:34
  • 1
    I will try that. I am new to Django. Will see how raw SQL works. Thank you very much! – Hamid Khan Feb 09 '18 at 16:41
0

I went with a loop in the template

# views.py
ms = M.objects.filter(foo=bar).all()
for m in ms:
    favorited = m.baz.qux_set.filter(foo=request.user.id).first()
    if favorited:
        m.tags = favorited.tags.names()

and

# template.html
{% for m in ms %}
  {% for tag in m.tags %}{{ tag }} , {% endfor %}
Harry Moreno
  • 10,231
  • 7
  • 64
  • 116