Skip to content

Instantly share code, notes, and snippets.

@bergantine
Last active December 8, 2022 19:13
Show Gist options
  • Select an option

  • Save bergantine/1346116 to your computer and use it in GitHub Desktop.

Select an option

Save bergantine/1346116 to your computer and use it in GitHub Desktop.
Django choice field and natural language display of that choice in templates. #django #djangotemplate
{% comment %}
Display the language representation of that choice instead of the numerical representation.
{% endcomment %}
{% for member in object_list %}
{{ member.get_team_display|lower }}
{% endfor %}
"""
For more info on choice fields see https://docs.djangoproject.com/en/dev/ref/models/fields/#field-choices
"""
class AccountTeamManager(models.Manager):
def get_query_set(self):
return super(AccountTeamManager, self).get_query_set().filter(team=self.model.ACCOUNT_TEAM)
class Team(models.Model):
LEADERSHIP_TEAM = 1
ACCOUNT_TEAM = 2
CREATIVE_TEAM = 3
TEAM_CHOICES = (
(LEADERSHIP_TEAM, 'Leadership'),
(ACCOUNT_TEAM, 'Account'),
(CREATIVE_TEAM, 'Creative'),
)
team = models.IntegerField(choices=TEAM_CHOICES, default=LEADERSHIP_TEAM)
...
# default model manager
objects = models.Manager()
# custom model managers
account_members = AccountTeamManager()
...
@franlu
Copy link

franlu commented Jun 1, 2016

👍
Thx!

@kayode-adechinan
Copy link

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment