Reputation: 117
In my Django form I have a ModelChoiceField for employees, but I want a choice in the dropdown for 'all' employees. What would be the best way to accomplish this?
employees = forms.ModelChoiceField(queryset=Employees.objects.all())
First attemptI tried
employees = forms.ChoiceField(choices = Employees.objects.values())
but I get a 'too many objects to unpack' error
Thanks
Upvotes: 0
Views: 624
Reputation: 6637
Try this:
employees = forms.ChoiceField(choices = [(emp['id'], emp['full_name'])
for emp in Employees.objects.values('id', 'full_name')])
The too many objects to unpack error raised because every cell of chioces must only contains two value similar below:
[(1, 'Eric Manson'), (2, 'Julia Rose'), (3, 'Saadi Khorshid'), ...]
But Employees.objects.values()
unpacking all fields in dictionary form.
Upvotes: 2