Robert Johnstone
Robert Johnstone

Reputation: 5371

Django Query Sets

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?

Upvotes: 2

Views: 140

Answers (2)

Mark Lavin
Mark Lavin

Reputation: 25154

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

Upvotes: 3

Yohann
Yohann

Reputation: 6336

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()

Upvotes: 0

Related Questions