Django how to display full username in FilteredSelectMultiple

I am trying to use the FilteredSelectMultiple widget to display a list of users. currently only the username is displayed. I tried to override label_from_instance as shown below, but it does not work. how it can display the full username.

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) 
+5
source share
6 answers

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

+2
source

The simplest solution is the following: models.py , where django.contrib.auth.models.User 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 __unicode__() method with a custom function that displays everything you want.

+14
source

I think you should override the unicode model method. You can create a new model, for example:

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

(for future reference)

Instead, you should subclass ModelMultipleChoiceField:

 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) ) 
+1
source

I did the same as jnns, and also redefined the _meta ordering list to order the list of users by their full username.

 # hack for displaying user 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 added this to the UserProfile model file. I do not think this is the best way to do this, but it is certainly very easy and practical. Tested in Django 1.4

For Django 1.5, after that it will be easier to do this, since we will have more control over the user model.

0
source

let's say your models

 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
source

Source: https://habr.com/ru/post/944264/


All Articles