i am trying to use FilteredSelectMultiple widget to display list of users. currently it is displaying only username. I have tried to override the label_from_instance as seen below but it does not seem to work. how can it get to display users full name.
class UserMultipleChoiceField(FilteredSelectMultiple):
"""
Custom multiple select Feild with full name
"""
def label_from_instance(self, obj):
return "%s" % (obj.get_full_name())
class TicketForm(forms.Form):
cc_to = forms.ModelMultipleChoiceField(queryset=User.objects.filter(is_active=True).order_by('username'), widget=UserMultipleChoiceField("CC TO", is_stacked=True)
The simplest solution is to put the following in a models.py where django.contrib.auth.models.User
is imported:
def user_unicode_patch(self):
return '%s %s' % (self.first_name, self.last_name)
User.__unicode__ = user_unicode_patch
This will overwrite the User
model's __unicode__()
method with a custom function that displays whatever you want.
A little late, but I think it might help people trying to solve a similar problem: http://djangosnippets.org/snippets/1642/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With