title: Django - Create a dropdown list from database
body: <p>I'm very new to Django and I would like to create a drop down box using distinct value of a column in a database:</p>
  
  <p>models.py:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I want a drop down list to contain all distinct values of sensor_loc_room and if the user select a room (sensor_loc_room), table displaying all sensors in the room will be displayed.</p>
  
  <p>forms.py</p>
  
  <pre><code>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())
  </code></pre>
  
  <p>This is my current view.py (only use to display a table of all sensors):</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>And index.html(only use to display a table of all sensors):</p>
  
  <pre><code>&lt;table&gt;
  
      {% for item in query_results %}
      &lt;tr&gt;
          &lt;td&gt;{{ item.sensor_uuid }}&lt;/td&gt;
          &lt;td&gt;{{ item.sensor_desc }}&lt;/td&gt;
          &lt;td&gt;{{ item.sensor_mid }}&lt;/td&gt;
          &lt;td&gt;{{ item.gw_uuid }}&lt;/td&gt;
          &lt;td&gt;{{ item.sensor_loc_blg }}&lt;/td&gt;
          &lt;td&gt;{{ item.sensor_loc_room }}&lt;/td&gt;
          &lt;td&gt;{{ item.sensor_loc_position }}&lt;/td&gt;
      &lt;/tr&gt;
      {% endfor %}
  &lt;/table&gt;
  </code></pre>
  
  <p>Any advise or guidance would be greatly appreciated. Thanks !</p>