I have a simple model like this
class UserType( models.Model ) :
def __unicode__( self ) :
return self.name
TYPE_CHOICES = (
( 'ad', 'administrator' ),
( 'mo', 'moderator' ),
( 'vi', 'viewer' ),
( 'pm', 'property manager' ),
( 'po', 'property owner' ),
( 'vm', 'vendor manager' ),
( 've', 'vendor' ),
( 'te', 'tenant' ),
)
name = models.CharField( max_length = 2, choices = TYPE_CHOICES )
In the admins.py
, I set filter_horizontal = ( 'user_types', )
for UserProfile
, which has a ManyToManyField
to UserType
. But in the UserProfile
admin page, the horizontal filter for M2M only shows the short-name of the tuple:
Also in a template, I want to show a list of user types a particular user has. So my template code looks like this
User type:
<ul>
{% if user_object.profile.user_types.all %}
{% for user_type in user_object.profile.user_types.all %}
<li>{{ user_type|capfirst }}</li>
{% endfor %}
{% else %}
<li>No user type</li>
{% endif %}
</ul>
And on the template, it displays only the short-name. I know normally I could show it the long-name by doing something like {{ get_user_type_display }}
, but in this case for M2M, it doesn't work.
So my question is two-fold:
- How do I display the long-name of a M2M choice/tuple in the admin page?
- How do I display the long-name in a M2M choice/tuple in a template?