Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django how to display users full name in FilteredSelectMultiple

Tags:

python

django

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)
like image 390
nashr rafeeg Avatar asked Sep 22 '11 07:09

nashr rafeeg


2 Answers

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.

like image 179
jnns Avatar answered Sep 19 '22 01:09

jnns


A little late, but I think it might help people trying to solve a similar problem: http://djangosnippets.org/snippets/1642/

like image 27
Artem Andreev Avatar answered Sep 21 '22 01:09

Artem Andreev