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 the Item model name fiel开发者_JS百科d has the similar values (but the other values of record are changed). 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 can I use?
I don't really understand your question. Do you want to select distinct values for name, as in
Item.objects.values('name').distinct()
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.
精彩评论