Last active
December 8, 2022 19:13
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {% 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 %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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() | |
| ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍
Thx!