When you are using models related by a Foreign Key, by default you have
access to the "children" models from the parent model. For example, your
Sensor model gets a "measurment_set" property just because of such foreign
key:

>>> dir(Sensor.objects.all()[0])
>
> [other properties, '*measurment_set*', [more properties]
>
>
>
which means you can access your measurements from a Sensor model:

>>> sensor = Sensor.objects.all()[0]
> >>> sensor
> <Sensor: id:1 / 2018-05-15>
> >>> sensor.measurment_set.all()
> <QuerySet [<Measurment: sens_id:1, time:2018-05-16 13:20:57.989089+00:00>,
> <Measurment: sens_id:1, time:2018-05-15 13:18:50.656182+00:00>]>
>

You can change the default name in the ForeignKey field declaration, using
the *related_name* parameter.

You can also access the children from the templates:

    <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>
>
>     {% *with sensor.measurment_set.all.0 as last_measurment* %}
>
>     <td>{{ last_measurment.temperature1 }}</td><td>{{
>> last_measurment.temperature2 }}</td><td>{{ last_measurment.temperature3
>> }}</td>
>
>     {% endwith %}
>
>       </tr>
>
>       {% endfor %}
>
>     </table>
>
>     {% else %}
>
>       <p>There are no sensors in the DB.</p>
>
>     {% endif %}
>
>
Using *with* limits your children to a single item, and telling the
template *all.0* you refer to the FIRST measurement for such sensor. The
only thing you need to do is tell the Measurment model to list itself by
time_of_measurment *reversed*, so the "first" object in the list will be
the latest measurement:


> class Measurment(models.Model):
> ....
>     class Meta:
>         ordering = ['-time_of_measurment',]


The way I have done it is a bit hacky (and I guess there are cleaner ways
to do it, using get_context or similars), but hey!


El mar., 15 may. 2018 a las 13:21, Anton Smirnov (<
antonsmirnov1...@gmail.com>) escribió:

> 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
> <https://groups.google.com/d/msgid/django-users/9250b3a0-eab8-4259-8cf6-00fb17975b18%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>


-- 
Fidel Leon
fi...@flm.cat
Phone: +34 622 26 44 92

-- 
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/CAHXg%3DN1mh8cxcVRxSOpQVwftFad8eOxRJUBmcJUA%2Bke7dR1uOA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to