Skip to content

Instantly share code, notes, and snippets.

@max747
Created January 7, 2020 06:13
Show Gist options
  • Save max747/f4c2f90a46bd9411b955040c34f7a7d5 to your computer and use it in GitHub Desktop.
Save max747/f4c2f90a46bd9411b955040c34f7a7d5 to your computer and use it in GitHub Desktop.
an example of models.TextChoices (django 3.0+)
% python ./manage.py shell
Python 3.7.6 (default, Dec 19 2019, 17:29:22)
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.db import models
>>>
>>> class FooChoices(models.TextChoices):
... Apple = 'A', 'りんご'
... Banana = 'B', 'バナナ'
... Orange = 'O', 'オレンジ'
...
>>>
>>> FooChoices.
FooChoices.Apple FooChoices.Orange FooChoices.labels FooChoices.names
FooChoices.Banana FooChoices.choices FooChoices.mro( FooChoices.values
>>>
>>> FooChoices.values
['A', 'B', 'O']
>>>
>>> FooChoices.names
['Apple', 'Banana', 'Orange']
>>>
>>> FooChoices.labels
['りんご', 'バナナ', 'オレンジ']
>>> FooChoices
<enum 'FooChoices'>
>>>
>>> FooChoices.choices
[('A', 'りんご'), ('B', 'バナナ'), ('O', 'オレンジ')]
>>>
>>> dict(FooChoices.choices)
{'A': 'りんご', 'B': 'バナナ', 'O': 'オレンジ'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment