43👍
✅
It looks like you may be looking for ModelChoiceField
.
user2 = forms.ModelChoiceField(queryset=User.objects.all())
This won’t show fullnames, though, it’ll just call __unicode__
on each object to get the displayed value.
Where you don’t just want to display __unicode__
, I do something like this:
class MatchForm(forms.Form):
user1 = forms.ChoiceField(choices = [])
def __init__(self, *args, **kwargs):
super(MatchForm, self).__init__(*args, **kwargs)
self.fields['user1'].choices = [(x.pk, x.get_full_name()) for x in User.objects.all()]
-1👍
class MatchForm(forms.Form):
choices = tuple(User.objects.all().values_list())
user1_auto = forms.CharField()
user1 = forms.ChoiceField(choices=choices)
user2_auto = forms.CharField()
user2 = forms.ChoiceField(choices=choices)
This should work.
👤Dami
- UWSGI listen queue of socket full
- NameError: global name 'logger' is not defined
- Why am I unable to run django migrations via the 'docker-compose run web' command?
Source:stackexchange.com