0

I'm just discovering Django and i'm still a total noob.

I got a view that return multiple query set.

class RouteDateView(ListView):
   model = Tblsummary
   template_name = 'reviewrouteassess/selectdaterange.html'

   def get_context_data(self, *args, **kwargs):
       context = super(RouteDateView, self).get_context_data(*args, **kwargs)

       context['allroute'] = Tblsummary.objects.filter(off_no=self.request.GET.get('depotcode')[:4])

       for p in context['allroute']:
          context[p.res_route] = Tblsummary.objects.filter(res_route=p.res_route)

       return context    

Then in my template I would like to access those dynamically name context. That's where i'm stuck.

<table>
{% for a in allroute %}
    <tr>
        <td>{{ a.res_route }}</td>
        <td>{{ a.hcalls }}</td>
    </tr>
    {% for d in a.res_route %}
        <tr>
            <td>{{ d.res_route }}</td>
            <td>{{ d.hcalls }}</td>
        </tr>
    {% endfor %}
{% endfor %}

How can I evaluate a.res_route so that a.res_route return the context passed by the view?? Thanks you so much!!

{% for d in a.res_route %}

Maquisard
  • 59
  • 1
  • 6
  • Django doesn't allow this in templates - see https://stackoverflow.com/questions/1275735/how-to-access-a-dictionary-element-in-a-django-template for a similar discussion. You'll want to change your view to construct your context such that the `a` in `{% for a in allroute %}` has everything it needs to render. – azundo Dec 16 '19 at 22:13
  • Ok thanks! I did read the discussion but i'm still confused. Can you point me in the good direction? Should I add a method to my model to return a sub queryset based on `res_route`? For your info i worked on a legacy database and I can't add any field to it, Should i use an abstract model to extend the Tblsummary method/property? Or should I use the `.items` to transform my context to a list? Ouf. Learning Django fell overwhelming to me right now. :) – Maquisard Dec 16 '19 at 23:05

1 Answers1

1

I would suggest adding a method to your model to access the second query:

class Tblsummary(models.Model):
    ... # or whatever is there currently

    def sub_routes(self):  # name this how you'd like
        return Tblsummary.objects.filter(res_route=self.res_route)

And then in your template:

<table>
{% for a in allroute %}
    <tr>
        <td>{{ a.res_route }}</td>
        <td>{{ a.hcalls }}</td>
    </tr>
    {% for d in a.sub_routes %}
        <tr>
            <td>{{ d.res_route }}</td>
            <td>{{ d.hcalls }}</td>
        </tr>
    {% endfor %}
{% endfor %}

This isn't going to be ideal from an efficiency standpoint since the sub_routes query is called once per entry in allroute but right now allroute is limited to 4 results so that shouldn't be an issue in practice.

azundo
  • 5,902
  • 1
  • 14
  • 21
  • Hey thanks! Really close to what I need! I know it's not efficiency ideal but it does the job! If i'm not mistaking `return Tblsummary.objects.filter(res_route=self.res_route)` query the whole table. Is there any way I can return only a subset of my queryset? Something like `return self.objects.filter(res_route=self.res_route)`?? Do I need a manager to do that? I've try to implement one but still with no success. `class SubTblsummaryManager(models.Manager): def get_queryset(self): return super().get_queryset()` – Maquisard Dec 17 '19 at 19:21