0

I have a model form and I'm trying to get a dropdown to populate the select with options from the database.

My model form looks like this:

class CreateTripsForm(forms.Form):
    start_locations = Mileage.objects.values('start_location').annotate(num_locations=Count('start_location')).order_by('start_location')
    end_locations = Mileage.objects.values('end_location').annotate(num_locations=Count('end_location')).order_by('end_location')
    starting_location = forms.ModelChoiceField(queryset=start_locations, empty_label=None)
    ending_location = forms.ModelChoiceField(queryset=end_locations, empty_label=None)

The select options are there, but they give results that are not what I'm after. The options in the select look like this:

{'start_location': 'Location A', 'num_locations': 27}
{'start_location': 'Location B', 'num_locations': 27}
{'start_location': 'Location C', 'num_locations': 27}

I just want the select to only show:

Location A
Location B
Location C

I've tried a number of different ways to accomplish this, but I feel I'm missing something.

Edit:

Mileage model looks like this:

class Mileage(models.Model):
    miles = models.DecimalField(max_digits=8, decimal_places=1)
    start_location = models.CharField(max_length=255)
    end_location = models.CharField(max_length=255)
    user_id = models.IntegerField(null=True)

    def __str__(self):
        return self.miles
Hanny
  • 2,078
  • 6
  • 24
  • 52
  • and what have you tried?? post that code. and also tell there are 2 choice fields, which is returning which ans? – Exprator May 31 '17 at 12:19
  • I've tried a few things unsuccessfully - very minor changes that ultimately would prove fruitless to provide here as they were small changes to essentially just this code. – Hanny May 31 '17 at 12:31
  • just tell me a thing, 2 fields have 2 different result right? {'start_location': 'Location A', 'num_locations': 27} or this is coming in whole for one field? – Exprator May 31 '17 at 12:32
  • Can you post your Mileage Model? Please include the `__str__` and/or `__unicode__` methods – Iain Shelvington May 31 '17 at 12:43
  • @Exprator - that whole thing is coming in for one field. I updated the question with my model. – Hanny May 31 '17 at 12:52

2 Answers2

0

I was able to get this working by changing my queryset.

Instead of using .values I changed it to .values_list('start_location', flat=True) and now it's giving me the list of the names I want in the select forms appropriately.

Hanny
  • 2,078
  • 6
  • 24
  • 52
  • maybe you need to use `.values_list('id', 'start_location', flat=True)`, to use the `id` as the `value` in the rendered options. – alfonso.kim May 31 '17 at 14:00
0

You want to display two selects with different properties of the same model. By default, ModelChoiceField uses the __str__ method of the QuerySet values, in this are dictionaries because you are calling values before the annotate.

Don't use values in the queryset, or use a ModelForm instead of ModelChoiceField

alfonso.kim
  • 2,844
  • 4
  • 32
  • 37
  • Yeah - I see now that I can use `values_list` to get a list and flatten it to get just pure values in list format instead of Tuples. Thanks! – Hanny May 31 '17 at 13:55