2

I have the following in one of my forms:

self.fields['advisor'].queryset = User.objects.filter(groups__name='advisor')

The only problem is that it displays the username in the drop down box. What I would like to do is display the first_name then last_name to make it more human readable.

Any ideas?

Robert Johnstone
  • 5,431
  • 12
  • 58
  • 88

2 Answers2

3

The ModelChoiceField has a label_from_instance method which can be changed in a subclass to use something other than the model __unicode__ method. https://docs.djangoproject.com/en/1.3/ref/forms/fields/#django.forms.ModelChoiceField.empty_label

Mark Lavin
  • 24,664
  • 5
  • 76
  • 70
  • I understanding what the docs are say, but then how would I use it in `forms.py`? – Robert Johnstone May 22 '12 at 21:57
  • Thanks... I get it now. I can implement it in `forms.py` with `advisor = UserModelChoiceField(queryset = User.objects.filter(groups__name='advisor'))`. Thanks to: http://stackoverflow.com/a/2959870/563247 – Robert Johnstone May 22 '12 at 22:09
0

According to this doc, try this quick and dirty code :

u = User.objects.filter(groups__name='advisor')
self.fields['advisor'].queryset = u.get_first_name_display() + " " + u.get_last_name_display()
Yohann
  • 6,047
  • 3
  • 26
  • 33