A
A
Andrey2015-09-03 18:36:43
Django
Andrey, 2015-09-03 18:36:43

How to properly group a queryset?

Hello!
Let's take for example:

class Car(models.Model):
    manufacturer = models.ForeignKey('Manufacturer')
    # ...

class Manufacturer(models.Model):
    # ...
    pass

How to group Car model entries by Manufacturer?
That is, in the template, I want to display records in this way:
{% for manufacturer, cars in car_list %}
<h1>{{ manufacturer.name }}</h1>
    {% for car in cars %}
        <p>{{ car.name }}</p>
    {% endfor %}
{% endfor %}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2015-09-03
@syschel

related_name

{% for man in m %}
<h1>{{ man.name }}</h1>
   {% for car in man.cars.all %}
        <p>{{ car.name }}</p>
    {% endfor %}
{% endfor %}

Or, as Maxim Chernyatevich pointed out,
the main thing is that we get manufactories and bring them out. And in them, cars are already pulling up with feedback.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question