2015-05-01 3 views
1

В моем шаблоне раскрывающийся список заполняется словарем, а не значением словаря. Как я могу сделать, что показано только значение?Django: показать значение словаря в раскрывающемся списке

Это мой collectionPoint

class CollectionPoint(models.Model): 
    addressID = models.AutoField(primary_key=True) 
    collectionPointName = models.CharField(max_length=50, null=False) 
    street = models.CharField(max_length=50, null=False) 
    streetnumber = models.CharField(max_length=20, null=False) 
    city = models.CharField(max_length=50, null=False) 
    postalcode = models.CharField(max_length=30, null=True) 
    gps_latitude = models.FloatField(blank=True, null=True) 
    gps_longitude = models.FloatField(blank=True, null=True) 
    country = models.ForeignKey(Country) 
    company = models.ForeignKey(Company) 

    #only the name is returned to the user 
    def __str__(self): 
     template = '{collectionPointName}' 
     return template.format(collectionPointName=self.collectionPointName) 

Я хочу, чтобы отобразить все отчетливое город из collectionpoints

class RentalSelectCityForm(forms.Form): 
    city = forms.ModelChoiceField(queryset=CollectionPoint.objects.order_by().values('city').distinct(),initial=0) 

Мой взгляд

@login_required 
def rentalselectcity(request): 
    # Get the context from the request. 
    context = RequestContext(request) 

    # A HTTP POST? 
    if request.method == 'POST': 
     form = RentalSelectCityForm(request.POST) 

     # Have we been provided with a valid form? 

     return HttpResponseRedirect('/') 
    else: 
     # If the request was not a POST, display the form to enter details. 
     form = RentalSelectCityForm() 

    context['path'] = [{'name': 'My rentals', 'url': reverse('rentals-list')}] 
    context['path'] += [{'name': 'Select city', 'url': reverse('rental-select-city')}] 

    # Bad form (or form details), no form supplied... 
    # Render the form with error messages (if any). 
    return render_to_response('user/rentalselectcity.html', {'form': form}, context) 

И мой шаблон

{% block content %} 
<div class="box box-default"> 
    <!--<div class="box-header"> 
     <h3 class="box-title">Title</h3> 
    </div>--><!-- /.box-header --> 
    <!-- form start --> 
    <form action="{% url 'rental-select-city' %}" method="post" role="form"> 
     {% csrf_token %} 
     <div class="box-body"> 
      {{ form.non_field_errors }} 
      <div class="form-group"> 
       {{ form.city.errors }} 
       <label for="{{ form.city.id_for_label }}">Select a city</label> 
       {{ form.city|attr:"class:form-control" }} 
      </div> 
     </div> 
     <!-- /.box-body --> 
     <div class="box-footer"> 
      <button type="submit" class="btn btn-primary">Select city</button> 
     </div> 
    </form> 
</div><!-- /.box --> 
{% endblock %} 

ответ

2

Вы можете изменить поля формы QuerySet к

CollectionPoint.objects.order_by().values_list('city', flat=True).distinct() 

Смотрите values_list docs для справки

+0

Это сделал это, спасибо – jdb