Hello!!
Can I come back to that issue.
Let me resume, what I need know.
I need to list all fields and for each field, I need to list the stations for that field.
For now, I have that views
def index(request):
"""Return the active fields."""
fields_list = Fields.objects.filter(field_active=1)
stations_list = Stations.objects.filter(station_active=1)
return render(request, 'map/slider.html', {'fields_list': fields_list, 'stations_list':stations_list})
On my template, I can easely list the fields and the stations. But ALL stations are listed for each field. I mean, all station for all fields are listed under each field. All is mixed.
I have 5 fields.
id_filed |
field name |
1 |
field 1 |
2 |
field 2 |
3 |
field 3 |
4 |
field 4 |
5 |
field 5 |
In my Stations table, I have a column named ‘fields_id_field’
If the station1 belong to field 2, the value of fields_id_field will be 2
If the station4 belong to field 5, the value of fields_id_field will be 5
etc.
Here are my two models:
class Fields(models.Model):
id_field = models.AutoField(primary_key=True)
countries_country = models.ForeignKey(Countries, models.DO_NOTHING)
states_id_state = models.ForeignKey('States', models.DO_NOTHING, db_column='states_id_state')
field_name = models.CharField(max_length=20)
field_longname = models.CharField(max_length=45, blank=True, null=True)
field_lat = models.DecimalField(max_digits=8, decimal_places=6, blank=True, null=True)
field_lng = models.DecimalField(max_digits=8, decimal_places=6, blank=True, null=True)
field_alt = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
threshold = models.IntegerField(blank=True, null=True)
city = models.CharField(max_length=45, blank=True, null=True)
cp = models.CharField(max_length=45, blank=True, null=True)
field_created = models.DateTimeField()
field_active = models.BooleanField()
def __str__(self):
return '[%s] %s' % (self.id_field, self.field_longname)
class Stations(models.Model):
id_station = models.AutoField(primary_key=True)
fields_id_field = models.ForeignKey(Fields, models.DO_NOTHING, db_column='fields_id_field')
stations_types_id_stations_type = models.IntegerField()
station_name = models.CharField(max_length=20)
station_longname = models.CharField(max_length=45, blank=True, null=True)
station_active = models.BooleanField()
station_archive = models.BooleanField()
station_lat = models.FloatField(blank=True, null=True)
station_lng = models.FloatField(blank=True, null=True)
station_alt = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
installed = models.DateTimeField()
station_description = models.TextField(blank=True, null=True)
ttn_app_id = models.CharField(max_length=20)
ttn_dev_id = models.CharField(max_length=20)
ttn_hardware_serial = models.CharField(max_length=20)
station_created = models.DateTimeField()
station_order = models.IntegerField()
map = models.BooleanField()
class Meta:
managed = False
db_table = 'stations'
Here is my template slider.html
If you look at the for section for the stations {% for station in stations_list %}
, it’s clear that Django can not list only the station for the corresponding field.
{% extends 'base.html' %}
{% block title %}
<title>EcoMap - (slider)</title>
{% endblock %}
{% block slider %}
<div>
{% if fields_list %}
<h4>Terrains</h4>
<ul>
{% for field in fields_list %}
<li>
<a class="nav-link" href="{% url 'map:stations' field.id_field %}">{{ field.field_name}}</a>
<ul>
{% for station in stations_list %}
<li>
<a href="{% url 'map:sensors' station.id_station %}">{{ station.id_station }}{{ station.station_name}} ({{ station.station_longname }}; Field: {{ station.fields_id_field }})</a>
</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
{% else %}
<p>No fields are available.</p>
{% endif %}
</div>
{% endblock %}
You previously wrote me, I do not need to do that, because Django can manage the relation.
<ul>
{% for station in stations_list %}
{% if station.fields_id_field.id_field == field.id_field %}
<li>
<a href="{% url 'map:sensors' station.id_station %}">{{ station.id_station }}{{ station.station_name}} ({{ station.station_longname }}; Field: {{ station.fields_id_field }})</a>
</li>
{% endif %}
{% endfor %}
</ul>
But what I missed, then to do? The relation is not done
(PS: I also tried looking at Making Query link you sent me, without success )
Many thanks