我对django刚起步(使用django 2.1.5和python 3.7),我似乎无法弄清楚如何添加一个下拉菜单,该菜单显示了我的postgresql数据库中一个表的一个字段的元素。我希望最终允许用户从下拉菜单中选择一个元素,然后我将使用该选择来构建查询并返回该查询的结果。但是我已经被下拉菜单所困扰。 这是我在models.py文件中的模型
class Locations(models.Model):
gid = models.AutoField(primary_key=True)
field_gid = models.BigIntegerField(db_column='__gid', blank=True, null=True) # Field renamed because it contained more than one '_' in a row. Field renamed because it started with '_'.
name_location= models.CharField(unique=True, max_length=254, blank=True, null=True)
x = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
y = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
z = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
geom = geomodels.PointField(srid=3912)
def __str__(self):
return self.name_location
class Meta:
managed = False
db_table = 'locations'
verbose_name_plural = "Locations"
这是forms.py文件中的表单:
from .models import Locations
class LocationsForm(forms.ModelForm):
class Meta:
model = Locations
fields = ('name_location',)
#Down here is what I actually thought I need, but maybe I am not using it right
locations_list = forms.ModelChoiceField(queryset=Locations.objects.all().only('name_location').order_by('name_location'))
然后,在我的views.py文件中:
from .forms import LocationsForm
def LocationsView(request):
form = LocationsForm
return render(request, 'geoportal/mforest.html', {'form': form})
我的urls.py如下:
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='geoportal-home'),
path('mforest/', views.LocationsView, name='geoportal-mforest')
]
最后在我的模板mforest.html文件中(我只放了一部分,因为base.html具有在mforest.html中扩展的块内容):
{% extends "geoportal/base.html" %}
{% block content %}
<div class="col-md-3" style="height: 100vh; border: 2px red; background-color: lightgray">
<h3>Meteo Stations of the SFI</h3>
<form method="GET">
{% csrf_token %}
{{ form }}
</form>
</div>
<div class="col-md-9">
<h3>Output of the query</h3>
<table width="100%" class="blueTable"> </table>
</div>
{% endblock content %}
对于模板文件,我还尝试了一些遍历查询集结果的建议。但是,我尝试过的所有解决方案都没有显示我的下拉菜单。我肯定我做错了,因为我没有得到预期的结果,但是我不知道要纠正什么。 PS。我没有收到错误。它根本不显示任何内容。 预先感谢您的帮助!
编辑:关于我想要实现的目标的更多信息,please click here可以查看页面上的内容(尽管几乎没有)。 (不要注意我在本问题中用“ name_location”替换的标签“ kraj_odvze”)。现在;我真正想要的是一个下拉菜单,而不是文本框,其中包含来自数据库中“ name_location”字段的元素。我还将在页面的右侧div上包括一个datetimerange选择器和一个按钮,以执行查询以表格和图形的形式呈现。
答案 0 :(得分:0)
由于@ art06的评论,我理解了这个问题。 由于我要查询另一个表(“ Meteo”),该表的一列表示指向“ Locations”表的“ name_location”列的foreign_key,因此我应该改用“ Meteo”表而不是“ Locations”
因此,将models.py文件作为以下文件:
class Locations(models.Model):
gid = models.AutoField(primary_key=True)
field_gid = models.BigIntegerField(db_column='__gid', blank=True, null=True) # Field renamed because it contained more than one '_' in a row. Field renamed because it started with '_'.
name_location = models.CharField(unique=True, max_length=254, blank=True, null=True)
x = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
y = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
z = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
geom = geomodels.PointField(srid=3912)
def __str__(self): # __unicode__ for py2
return self.name_location
class Meta:
managed = False
db_table = 'locations'
verbose_name_plural = "Locations"
class Meteo(models.Model):
time = models.DateTimeField()
air_temp = models.FloatField(blank=True, null=True)
soil_temp = models.FloatField(blank=True, null=True)
precipitation = models.FloatField(blank=True, null=True)
global_rad = models.FloatField(blank=True, null=True)
relative_hum = models.FloatField(blank=True, null=True)
wind_speed = models.FloatField(blank=True, null=True)
swc = models.FloatField(blank=True, null=True)
pressure = models.FloatField(blank=True, null=True)
location = models.ForeignKey(Locations, models.DO_NOTHING, db_column='location', to_field='name_location') # to_field is required because the Foreignkey field is a text
id = models.BigAutoField(primary_key=True)
class Meta:
managed = False
db_table = 'meteo'
unique_together = (('time', 'id'),)
verbose_name_plural = "Meteo"
和forms.py如下:
from django import forms
from .models import Meteo
class LocationsForm(forms.ModelForm):
class Meta:
model = Meteo
fields = ('location',)
通过更改帖子中的先前内容,django将字段格式化为下拉菜单,并且显示效果很好。
因此,我在这里不正确的要点是: 即使不编写ModelChoiceField,当将ModelForm与外键一起使用时,它也会自动创建一个下拉菜单。