Mysql Django-从数据库创建下拉列表
我是Django的新手,我想使用数据库中列的不同值创建一个下拉框: models.py:Mysql Django-从数据库创建下拉列表,mysql,django,dropdown,Mysql,Django,Dropdown,我是Django的新手,我想使用数据库中列的不同值创建一个下拉框: models.py: from django.db import models class SensorsTable(models.Model): sensor_uuid = models.CharField(primary_key=True, max_length=32) sensor_desc = models.CharField(max_length=256) sensor_mid = model
from django.db import models
class SensorsTable(models.Model):
sensor_uuid = models.CharField(primary_key=True, max_length=32)
sensor_desc = models.CharField(max_length=256)
sensor_mid = models.CharField(max_length=256)
gw_uuid = models.CharField(max_length=32, blank=True, null=True)
sensor_loc_lat = models.DecimalField(max_digits=11, decimal_places=8, blank=True, null=True)
sensor_loc_long = models.DecimalField(max_digits=11, decimal_places=8, blank=True, null=True)
sensor_loc_blg = models.CharField(max_length=100, blank=True, null=True)
sensor_loc_room = models.CharField(max_length=100, blank=True, null=True)
sensor_loc_position = models.CharField(max_length=100, blank=True, null=True)
class Meta:
managed = False
db_table = 'sensors_table'
def __str__(self):
return self.sensor_uuid
class SensorsTable(models.Model):
sensor_uuid = models.CharField(primary_key=True, max_length=32)
sensor_desc = models.CharField(max_length=256)
sensor_mid = models.CharField(max_length=256)
gw_uuid = models.CharField(max_length=32, blank=True, null=True)
sensor_loc_lat = models.DecimalField(max_digits=11, decimal_places=8, blank=True, null=True)
sensor_loc_long = models.DecimalField(max_digits=11, decimal_places=8, blank=True, null=True)
sensor_loc_blg = models.CharField(max_length=100, blank=True, null=True)
sensor_loc_room = models.CharField(max_length=100, blank=True, null=True)
sensor_loc_position = models.CharField(max_length=100, blank=True, null=True)
class Meta:
managed = False
db_table = 'sensors_table'
def __str__(self):
return self.sensor_uuid
我想要一个下拉列表来包含sensor_loc_room的所有不同值,如果用户选择一个房间(sensor_loc_room),则将显示显示房间中所有传感器的表格
forms.py
from django.forms import ModelChoiceField
from django import forms
from .models import *
class LocationChoiceField(forms.ModelForm):
#locations= forms.ModelChoiceField(queryset=InputLocation.objects.all())
locations = forms.ModelChoiceField(queryset=SensorsTable.objects.all().values_list("sensor_loc_blg").distinct())
from django import forms
from .models import *
class LocationChoiceField(forms.Form):
locations = forms.ModelChoiceField(
queryset=SensorsTable.objects.values_list("sensor_loc_blg", flat=True).distinct(),
empty_label=None
)
这是my current view.py(仅用于显示所有传感器的表格):
和index.html(仅用于显示所有传感器的表格):
{查询结果%中的项的百分比}
{{item.sensor_uuid}
{{item.sensor_desc}}
{{item.sensor_mid}
{{item.gw_uuid}
{{item.sensor_loc_blg}}
{{item.sensor_loc_room}
{{item.sensor_loc_position}
{%endfor%}
如有任何建议或指导,将不胜感激。谢谢 我设法使用模型中列的不同值创建了一个下拉框。如果有人正在寻找答案: models.py:
from django.db import models
class SensorsTable(models.Model):
sensor_uuid = models.CharField(primary_key=True, max_length=32)
sensor_desc = models.CharField(max_length=256)
sensor_mid = models.CharField(max_length=256)
gw_uuid = models.CharField(max_length=32, blank=True, null=True)
sensor_loc_lat = models.DecimalField(max_digits=11, decimal_places=8, blank=True, null=True)
sensor_loc_long = models.DecimalField(max_digits=11, decimal_places=8, blank=True, null=True)
sensor_loc_blg = models.CharField(max_length=100, blank=True, null=True)
sensor_loc_room = models.CharField(max_length=100, blank=True, null=True)
sensor_loc_position = models.CharField(max_length=100, blank=True, null=True)
class Meta:
managed = False
db_table = 'sensors_table'
def __str__(self):
return self.sensor_uuid
class SensorsTable(models.Model):
sensor_uuid = models.CharField(primary_key=True, max_length=32)
sensor_desc = models.CharField(max_length=256)
sensor_mid = models.CharField(max_length=256)
gw_uuid = models.CharField(max_length=32, blank=True, null=True)
sensor_loc_lat = models.DecimalField(max_digits=11, decimal_places=8, blank=True, null=True)
sensor_loc_long = models.DecimalField(max_digits=11, decimal_places=8, blank=True, null=True)
sensor_loc_blg = models.CharField(max_length=100, blank=True, null=True)
sensor_loc_room = models.CharField(max_length=100, blank=True, null=True)
sensor_loc_position = models.CharField(max_length=100, blank=True, null=True)
class Meta:
managed = False
db_table = 'sensors_table'
def __str__(self):
return self.sensor_uuid
forms.py
from django.forms import ModelChoiceField
from django import forms
from .models import *
class LocationChoiceField(forms.ModelForm):
#locations= forms.ModelChoiceField(queryset=InputLocation.objects.all())
locations = forms.ModelChoiceField(queryset=SensorsTable.objects.all().values_list("sensor_loc_blg").distinct())
from django import forms
from .models import *
class LocationChoiceField(forms.Form):
locations = forms.ModelChoiceField(
queryset=SensorsTable.objects.values_list("sensor_loc_blg", flat=True).distinct(),
empty_label=None
)
views.py(最简单的方式)
index.html
<body>
<div class="container">
<p></p>
<form method=POST action="">
{{ location_list }}
</form>
<h1>All sensors</h1>
<table>
{% for item in query_results %}
<tr>
<td>{{ item.sensor_uuid }}</td>
<td>{{ item.sensor_desc }}</td>
<td>{{ item.sensor_mid }}</td>
<td>{{ item.gw_uuid }}</td>
<td>{{ item.sensor_loc_blg }}</td>
<td>{{ item.sensor_loc_room }}</td>
<td>{{ item.sensor_loc_position }}</td>
</tr>
{% endfor %}
</table>
</div>
</body>
{{location_list}}
所有传感器
{查询结果%中的项的百分比}
{{item.sensor_uuid}
{{item.sensor_desc}}
{{item.sensor_mid}
{{item.gw_uuid}
{{item.sensor_loc_blg}}
{{item.sensor_loc_room}
{{item.sensor_loc_position}
{%endfor%}
也许我真的很累,但我想这就是你想要的。