There are two models "Sensors" with information about them and 
"Measurments" .

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)

>From each sensor I serially recive measurments. On the page, it is 
necessary to display the sensor data and the latest measurement data from 
the "Measurments" model corresponding for each sensor.

for displaying data about sensor I used ListView:

view:

from .models import Sensor, Measurmentclass 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 sens<h4></td>
    <td><h4>Date of install<h4></td>
    <td><h4>Latitude<h4></td>
    <td><h4>Longitude<h4></td>
    <td><h4>Data from last measurments<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 dont know How to display data about last measurments. I think need 
using aggregate to calculate max of time_of_measurment for each id_sens.

How it can be done?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/9250b3a0-eab8-4259-8cf6-00fb17975b18%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to