Problem returning context_processor data to html for looping

I'm trying to add categories field to navigation. So far I've made context_processor which querys all the car brands. In my navigation I'm loopin all of the brands and this works. I also want to add dropdowns which contains certain car models for certain car brands. I think the problem lays in returning right data, that is loopable in my _base.html.

_base.html

        <div class="flex pt-3 container w-full">

          {% for item in car_brands %}

            <button id="dropdown-button" data-dropdown-toggle="dropdown-{{item.id}}" class="py-2.5 px-4 text-sm font-medium text-center text-gray-900 bg-gray-100 uppercase" type="button">{{item}}</button>
            <div id="dropdown-{{item.id}}" class="hidden shadow w-44 bg-gray-100">
              <ul aria-labelledby="dropdown-button">
                {% for model in models %}
                <li>
                  <a href="#" class="inline-flex w-full px-4 py-2 hover:bg-gray-50">{{ model }}</a>
                </li>
                {% endfor %}
              </ul>
            </div>

          {% endfor %}

context_processors.py

from .models import CarBrandCategory, CarModel

def brand_catalogue(request):
    car_brands = CarBrandCategory.objects.all()


    for brand in car_brands:
        models = [model for model in CarModel.objects.filter(brand_id=brand.id)]
        print(models)


    return {
        'car_brands': car_brands,
        'models': models
        }

If printing, it will give me different lists, consists of different models, eg. [<CarModel: X4>, <CarModel: X5>, <CarModel: 3. Seeria>] [<CarModel: A4>, <CarModel: A6>, <CarModel: A8>] [<CarModel: C klass>, <CarModel: S-Klass>]

I was wondering if anyone has experience with this kind of solutions and what could be the most optimal way to create this kind of solution?

Thanks

I've created a list of different car models, but I'm unable to return it to html. I want each car brand in my naviagion menu to have dropdown, which consists of certain car models.

You are overcomplicating things, you can work with:

from .models import CarBrandCategory


def brand_catalogue(request):
    return {'car_brands': CarBrandCategory.objects.prefetch_related('model')}

Then in the template, you use:

<ul>
    {% for brand in car_brands %}
        <li>{{ brand.brand_name }}
        {% for model in brand.model.all %}
            <li>{{ model.model }}</li>
        {% endfor %}
        </li>
    {% endfor %}
</ul>
Back to Top