Answer the question
In order to leave comments, you need to log in
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)
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')
{% 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 %}
Answer the question
In order to leave comments, you need to log in
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')
{% 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 questionAsk a Question
731 491 924 answers to any question