2017-08-10 73 views
1

一個下拉列表,我很新的Django和我想創建一個數據庫使用列的不同值的下拉框: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 = 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()) 

這是我目前view.py(僅用於顯示所有傳感器的表):

from django.shortcuts import render 

# Create your views here. 

from django.http import HttpResponse 
from django.template import loader 
from django.shortcuts import render 
from django.views.generic.edit import CreateView 


from .models import * 


#def index(request): 
# return HttpResponse("Smart Uni Analytics Test") 


def index(request): 
    query_results = SensorsTable.objects.all() 
    locationForm = LocationChoiceField() 

    context = { 
     'query_results': query_results, 

    } 
    return render(request,'analytics/index.html', context) 

和index.html(僅用於顯示一個所有傳感器表):

<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> 

任何意見或指導將不勝感激。謝謝 !

+0

也許我只是真的很累,但我認爲[這](https://stackoverflow.com/questions/16055808/django-form-dropdown-list-of-stored-models)是你在找什麼。 –

回答

0

我設法創建一個下拉框,使用模型中列中的不同值。如果有人正在尋找一個答案:

models.py:

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 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(最簡單的方法)

from django.http import HttpResponse 
from django.template import loader 
from django.shortcuts import render 
from django.views.generic.edit import CreateView 
from .forms import * 

from .models import * 



def index(request): 
    query_results = SensorsTable.objects.all() 
    location_list = LocationChoiceField() 

    context = { 
     'query_results': query_results, 
     'location_list': location_list, 

    } 
    return render(request,'analytics/index.html', context) 

指數.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>