views:

53

answers:

2

I have models...

class Item(models.Model):
    name = models.CharField('Item Name', max_length = 30)
    item_code = models.CharField(max_length = 10)
    color = models.CharField(max_length = 150, null = True, blank = True)
    size = models.CharField(max_length = 30, null = True, blank = True)
    fabric_code = models.CharField(max_length = 30, null = True, blank = True)    

I have values in Item. in Item model name field has the similar values..(but the other values of record are change). I want to select the name field values distinctly(ie similar values select only ones). in one box(like combo box).

What kind of form or views i use??

A: 

I don't really understand your question. Do you want to select distinct values for name, as in

Item.objects.values('name').distinct()
jensq
yes, i want similar this. but how can i use it with in form ??likeitem = forms.ModelChoiceField(label = 'Select Item', queryset = Item.objects.values('name').distinct()) it does not work.)
qulzam
A: 

if you want to change a widget choices items, use something like this :

choices_list = Item.objects.values_list('name','name').distinct()
form_item = forms.ModelChoiceField(label = 'Select Item', choices = choices_list)

As said in the django field docs :

choices : An iterable (e.g., a list or tuple) of 2-tuples to use as choices for this field.

jujule
I use this but i also want to add the first empty field? the above code does not add empty value
qulzam
choices_list = list(Item.objects.values_list('name','name').distinct())choices_list.insert(0, ('', '------'))
jujule