A
A
Anton2018-05-14 13:05:29
Django
Anton, 2018-05-14 13:05:29

Simultaneous display of data from two models, how to arrange view and template?

The following models are available, the "Sensors" sensor information model, and the "Measurments" sensor data model .

class Sensor(models.Model):
    date_start = models.DateField()
    Latitude =  models.DecimalField(max_digits=18, decimal_places=15)
    Longitude = models.DecimalField(max_digits=18, decimal_places=15)

    def __str__(self):
        return 'id:%s / %s' % (self.id, self.date_start)

class Measurment(models.Model):
    sens = models.ForeignKey(Sensor, on_delete=models.PROTECT)
    time_of_measurment = models.DateTimeField()
    humidity = models.PositiveSmallIntegerField()
    temperature1 = models.DecimalField(max_digits=5, decimal_places=2)
    temperature2 = models.DecimalField(max_digits=5, decimal_places=2)
    temperature3 = models.DecimalField(max_digits=5, decimal_places=2)

    def __str__(self):
        return 'sens_id:%s, time:%s' % (self.sens.id, self.time_of_measurment)

Data is received periodically from each sensor. On the page, you need to display data about the sensor and the data of the last measurement from the "Measurments" model corresponding to this sensor.
I sort of figured out the display of sensor data using ListView:
view:
from .models import Sensor, Measurment
class SenorsListView(generic.ListView):
    model = Sensor, Measurment
    context_object_name = 'sensors_list'
    template_name = 'sensors_list.html'
    queryset = Sensor.objects.all().order_by('-date_start')

template "sensors_list.html":
{% extends "base_generic.html" %}
{% block content %}
    <h1>Sensors List</h1>
    {% if sensors_list %}
    <table class="table">
  <tr>
    <td><h4>ID датчика<h4></td>
    <td><h4>Дата установки<h4></td>
    <td><h4>Долгота<h4></td>
    <td><h4>Широта<h4></td>
    <td><h4>Данные последнего измерения<h4></td>
  </tr>
      {% for sensor in sensors_list %}
      <tr>
    <td>{{sensor.id}}</td>
    <td>{{sensor.date_start}}</td>
   <td>{{sensor.Latitude}}</td>
    <td>{{sensor.Longitude}}</td>
    <td>{{}}</td>
      </tr>
      {% endfor %}
    </table>
    {% else %}
      <p>There are no sensors in the DB.</p>
    {% endif %} 
{% endblock %}

But I can't figure out how else to pull exactly the right data from the "Measurments" model.
Please, tell me how can I solve this problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JonGalt, 2018-05-24
@JonGalt

I don't see a problem at all.
Making a regular ListView with a Senors model

from .models import Sensor
from .models import Sensor, Measurment
class SenorsListView(generic.ListView):
    model = Sensor
    context_object_name = 'sensors_list'
    template_name = 'sensors_list.html'
    queryset = Sensor.objects.all().order_by('-date_start')

and in the template do this
{% extends "base_generic.html" %}
{% block content %}
    <h1>Sensors List</h1>
    {% if sensors_list %}
    <table class="table">
  <tr>
    <td><h4>ID датчика<h4></td>
    <td><h4>Дата установки<h4></td>
    <td><h4>Долгота<h4></td>
    <td><h4>Широта<h4></td>
    <td><h4>Данные последнего измерения<h4></td>
  </tr>
      {% for sensor in sensors_list %}
      <tr>
    <td>{{sensor.id}}</td>
    <td>{{sensor.date_start}}</td>
   <td>{{sensor.Latitude}}</td>
    <td>{{sensor.Longitude}}</td>
    # Данные последнего датчика
    <td>{{sensor.measurment_set.last.temperature1}}</td>
      </tr>
      {% endfor %}
    </table>
    {% else %}
      <p>There are no sensors in the DB.</p>
    {% endif %} 
{% endblock %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question