E.Mohammed
E.Mohammed

Reputation: 163

django template "grouped" tag don't gives me the grouped records as I wan't

I'm working on a project on django, I'm using the built in view ArchiveListView, and I want to sort records to be grouped by a common attribute.
So django ORM don't have a group by query, so I tried to do this on the template using regroup tag, but I think it take a long time and it doesn't give me the grouped record as I want this is the models I have :

class Vehicle(models.Model):
    serie = models.CharField(max_length=18, unique=True, blank=True, null=True)
    matricule = models.CharField(max_length=18, unique=True, blank=True, null=True)
    ww = models.CharField(max_length=18,blank=True, null=True)
    marque_id = models.ForeignKey('Marque', blank=True, null=True, on_delete=models.SET_NULL)
    entry_into_service = models.DateField(auto_now=False, auto_now_add=False, blank=True, null=True)
    mouchard = models.DateField(auto_now=False, auto_now_add=False, blank=True, null=True)
    extinguisher = models.DateField(auto_now=False, auto_now_add=False, blank=True, null=True, help_text="extincteur expiratiOn date")
    city_id = models.ForeignKey('City', blank=True, null=True, on_delete=models.SET_NULL)
    region_id = models.ForeignKey('Region', blank=True, null=True, on_delete=models.SET_NULL)
    driver_id = models.ForeignKey('Driver', blank=True, null=True, on_delete=models.SET_NULL)
    class Meta:
        ordering = ['serie','matricule']

    def __str__(self):
        return (self.matricule)

class GazStation(models.Model):
    name = models.CharField(max_length=128, blank=True)
    city_id = models.ForeignKey('City',  blank=True, null=True, on_delete=models.SET_NULL)
    geo_localization = gis_models.PointField(blank=True, null=True)
    class Meta:
        ordering = ['city_id','name']
    def __str__(self):
        return '%s %s' % (self.name, self.city_id.city)
class Refuel(models.Model):
    vehicle_id = models.ForeignKey('Vehicle', blank=True, null=True, on_delete=models.SET_NULL)
    driver_id = models.ForeignKey('Driver', blank=True, null=True, on_delete=models.SET_NULL, limit_choices_to ={'is_driver':True})
    Controlor_id = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, related_name='controlor_refuel', on_delete=models.SET_NULL, limit_choices_to ={'is_controlor':True})
    gaz_station_id = models.ForeignKey('GazStation', blank=True, null=True, on_delete=models.SET_NULL)
    date_time_creation = models.DateTimeField(auto_now=False, auto_now_add=True)
    date_time_modified = models.DateTimeField(auto_now=True, blank=True)
    odometer_reading = models.PositiveIntegerField(blank=True)
    fuel_quantity = models.DecimalField(max_digits=5, decimal_places=2)
    fuel_unit_price = models.DecimalField(max_digits=6, decimal_places=2)
    class Meta:
        ordering = ['gaz_station_id','-date_time_creation']
    def __str__(self):
        return (self.vehicle_id.serie)

and this is the class view I'm using:


####### refuel view filtred by year  #################
class RefuelsListViewSup(LoginRequiredMixin, YearArchiveView):
    queryset = Refuel.objects.order_by('vehicle_id').all()
    date_field = "date_time_creation"
    make_object_list = True
    allow_future = True

and the template to display this view is:

{% extends 'base.html' %}
{% load mathfilters %}
{% block title %}
    <title>Refuels Archive</title>
{% endblock title %}
{% block content %}
          <div class="container">
                  <h1>La liste des gazoile archivé </h1>
              <ul>
                  {% for date in date_list %}
              <li>{{ date|date }}</li>
                  {% endfor %}
              </ul>
          <div>
        <h1>All Refuels for {{ year|date:"Y" }}</h1>
        {% regroup object_list by vehicle_id as vehicle_refuel %}
        <ul>
          {% for vehicle_id in vehicle_refuel %}
            <p>
              <li>Les pleints fait pour {{ vehicle_id.grouper}} sont:</li>
            </p>
            <table border="2">
              <thead class="table-dark">
                <th colspan="2">Driver name :</th>
                <th>Driver number :</th>
                <th>Gaz Station : </th>
                <th>Date :</th>
                <th>Kilometrage :</th>
                <th>Quantity of Fuel: </th>
                <th>Litre Price (Dh): </th>
                <th>Totale (Dh)</th>
                <th>City</th>
              </thead>
              <tbody id="body-table"><tr>
              {% for refuel in vehicle_id.list %}
              <tr>
                <td colspan="2">{{ refuel.driver_id.first_name}}  {{ refuel.driver_id.last_name }}</td>
                <td>{{ refuel.driver_id.registration_number }}</td>
                <td>{{ refuel.gaz_station_id.name}}</td>
                <td>{{ refuel.date_time_creation }}</td>
                <td>{{ refuel.odometer_reading }}</td>
                <td>{{ refuel.fuel_quantity }}</td>
                <td>{{ refuel.fuel_unit_price }}</td>
                <td>{{ refuel.fuel_quantity|mul:refuel.fuel_unit_price }}</td>
                <td>{{ refuel.gaz_station_id.city_id.city }}</td>
              </tr>
              {% endfor %}
            </tbody>
            {% endfor %}
        </ul>

          </div>
        </div>

        {% endblock content %}

Upvotes: 0

Views: 37

Answers (1)

Daniel Oram
Daniel Oram

Reputation: 8411

You have two errors in your html

random <tr> element with no closing tag.

<tbody id="body-table"><tr>
              {% for refuel in vehicle_id.list %}

no closing tag for <table>

          </tr>
          {% endfor %}
        </tbody>
<!-- missing </table> here-->
      {% endfor %}
    </ul>

Because I can't run your code I have assumed that the <tr> tag is not supposed to be there.

Therefore, hopefully this fixes your problem.

{% extends 'base.html' %}
{% load mathfilters %}
{% block title %}
<title>Refuels Archive</title>
{% endblock title %}
{% block content %}
<div class="container">
  <h1>La liste des gazoile archivé </h1>
  <ul>
    {% for date in date_list %}
    <li>{{ date|date }}</li>
    {% endfor %}
  </ul>
  <div>
    <h1>All Refuels for {{ year|date:"Y" }}</h1>
    {% regroup object_list by vehicle_id as vehicle_refuel %}
    <ul>
      {% for vehicle_id in vehicle_refuel %}
      <p>
        <li>Les pleints fait pour {{ vehicle_id.grouper}} sont:</li>
      </p>
      <table border="2">
        <thead class="table-dark">
          <th colspan="2">Driver name :</th>
          <th>Driver number :</th>
          <th>Gaz Station : </th>
          <th>Date :</th>
          <th>Kilometrage :</th>
          <th>Quantity of Fuel: </th>
          <th>Litre Price (Dh): </th>
          <th>Totale (Dh)</th>
          <th>City</th>
        </thead>
        <tbody id="body-table">
          {% for refuel in vehicle_id.list %}
          <tr>
            <td colspan="2">{{ refuel.driver_id.first_name}} {{ refuel.driver_id.last_name }}</td>
            <td>{{ refuel.driver_id.registration_number }}</td>
            <td>{{ refuel.gaz_station_id.name}}</td>
            <td>{{ refuel.date_time_creation }}</td>
            <td>{{ refuel.odometer_reading }}</td>
            <td>{{ refuel.fuel_quantity }}</td>
            <td>{{ refuel.fuel_unit_price }}</td>
            <td>{{ refuel.fuel_quantity|mul:refuel.fuel_unit_price }}</td>
            <td>{{ refuel.gaz_station_id.city_id.city }}</td>
          </tr>
          {% endfor %}
        </tbody>
      </table>
      {% endfor %}
    </ul>

  </div>
</div>

{% endblock content %}

Upvotes: 1

Related Questions