开发者

django how to display users full name in FilteredSelectMultiple

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('userna开发者_如何学Pythonme'), 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.


(For future reference)

You should subclass ModelMultipleChoiceField instead:

class UserMultipleChoiceField(forms.ModelMultipleChoiceField):
    """
    Custom multiple select Feild with full name
    """
    def label_from_instance(self, obj):
          return obj.get_full_name()


class TicketForm(forms.Form):
    cc_to  = UserMultipleChoiceField(
            queryset=User.objects.filter(is_active=True).order_by('username'),
            widget=FilteredSelectMultiple("CC TO", is_stacked=True)
        )

Another solution is to subclass User and use it in your query set (as in this question: Django show get_full_name() instead or username in model form)

class UserFullName(User):
    class Meta:
        proxy = True
        ordering = ["username"]

    def __unicode__(self):
        return self.get_full_name()

class TicketForm(forms.Form):
    cc_to  = forms.ModelMultipleChoiceField(
            queryset=UserFullName.objects.filter(is_active=True),
            widget=FilteredSelectMultiple("CC TO", is_stacked=True)
        )


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


I think you have to override User model unicode method. You can create new model, like this:

class ExtendedUser(User): # inherits from django.contrib.auth.models.User
    def __unicode__(self):
        return '%s %s' % (self.first_name, self.last_name)


I made the same as jnns and also override the _meta ordering list, to order users list by their user full name.

# hack for displaying user's full name instead of username and order them by full name
def user_unicode_patch(self):
    return '%s %s' % (self.first_name, self.last_name)
User.__unicode__ = user_unicode_patch
User._meta.ordering = ['first_name', 'last_name']

I've added this at the UserProfile model file. I don't thing this is the best way of doing that, but for sure is very easy and practical. Tested in Django 1.4

For Django 1.5 after, it will be easier to do that, since we'll have more control of the User model.


let's say your models are

class UserProfile(models.Model) :
    first_name = models.CharField(max_length=255, blank=True)
    second_name = models.CharField(max_length=255, blank=True)

    def full_name(self):
        """

        : Return the first_name plus the last_name, with space in between.
        """
        full_name = '%s %s' % (self.first_name, self.second_name)
        return full_name.strip()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜