Hi Ken, Thanks for being so kind to response to my questions. I just started with Django. Did some basic python course and a ‘Getting Started’ Django’ Youtube course. I’m not at all familiar with queryset and convert into a dict. Can you advise me what road I should follow in order to get familiar with Django app development. I’m a bit impatient and that causes getting stuck like what is happening now. So a good learning path advice would be helpfull.
Beyond that, it’s a highly individual decision, based upon what concepts an individual may be struggling with at that point. If there’s anything I’ve learned as a developer, teacher, and mentor, is that there’s no “one size fits all” category of learning materials. Different books resonate with different people.
Find the appropriate database function for this, and annotate the Weeknr in the query:
Shift.objects.all().annotate(Weeknr=<some database function>)
Note: Since this work is being done in the database, it’s not capable of using the function that you defined.
Return the queryset as a dict and augment the dict with the desired values:
shifts = Shift.objects.all().values()
for a_shift in shifts:
a_shift['Weeknr'] = ....
I will point out that this isn’t going to be the most efficient way of doing this. Doing “work” in the template is always my last choice - my preference is to do all possible work in the view or database, not the template.
Now, it’s very likely that this is going to be absolutely fine for you. But try to keep this in mind in the future if you find that this page is taking too long to render.
One more thing. Can I add another field to the group header? EG I like to add start_date to the grouper value. I can’t get that working.
{% regroup cities by country as country_list %}
<ul>
{% for country in country_list %}
<li>{{ country.grouper }} **{{add another field here }}**
<ul>
{% for city in country.list %}
<li>{{ city.name }}: {{ city.population }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
You would need to make sure that that value is being supplied to the template through the context.
I’d need to see the current view to be able to answer your question. Or, it may be easiest for you to just render country_list immediately after the regroup to see what keys are available and under what names.
Can you help me how this render code should look like. This is my real data:
{% regroup shifts by weeknr as week_list %}
{% render week_list %}?????
I am still struggeling with the regroup because I need a multi level regroup. I would like to try an If else approach to build my html is that a good choice?