Skip to content

Instantly share code, notes, and snippets.

@ishtiaque2asad2
Forked from JackAtOmenApps/django_choices.txt
Created October 21, 2022 03:58
Show Gist options
  • Select an option

  • Save ishtiaque2asad2/77437a39653da299fe1fba473a6a6454 to your computer and use it in GitHub Desktop.

Select an option

Save ishtiaque2asad2/77437a39653da299fe1fba473a6a6454 to your computer and use it in GitHub Desktop.

Revisions

  1. @JackAtOmenApps JackAtOmenApps revised this gist May 13, 2021. 1 changed file with 13 additions and 10 deletions.
    23 changes: 13 additions & 10 deletions django_choices.txt
    Original file line number Diff line number Diff line change
    @@ -9,22 +9,23 @@ class Animal(models.Model):

    animal_type = models.CharField(max_length=1, choices=AnimalType.choices, default=AnimalType.ANTELOPE)

    >>> # Working with the AnimalType class itself
    >>> some_animal_type = Animal.AnimalType
    # In this example, the middle value on each line is what is saved to the database. So, `B` if I selected Bat. Since I am only using one character for each possible entry in this example, I set the `max_length` to 1.

    >>> # Working with the AnimalType class itself outside of the Animal model
    >>>
    >>> some_animal_type.values
    >>> Animal.AnimalType.values
    ['A', 'B', 'C']
    >>>
    >>> some_animal_type.names
    >>> Animal.AnimalType.names
    ['ANTELOPE', 'BAT', 'COUGAR']
    >>>
    >>> some_animal_type.labels
    >>> Animal.AnimalType.labels
    ['Antelope', 'Bat', 'Cougar']
    >>>
    >>> some_animal_type.choices
    >>> Animal.AnimalType.choices
    [('A', 'Antelope'), ('B', 'Bat'), ('C', 'Cougar')]
    >>>
    >>> dict(some_animal_type.choices)
    >>> dict(Animal.AnimalType.choices)
    {'A': 'Antelope', 'B': 'Bat', 'C': 'Cougar'}
    >>>
    >>> # Working with AnimalType in a Model Instance
    @@ -33,9 +34,11 @@ class Animal(models.Model):
    >>>
    >>> my_animal_type = my_animal.animal_type
    >>>
    >>> my_animal.animal_type
    >>> # To get the value saved to the database:
    >>> str(my_animal.animal_type)
    'A'
    >>>
    >>> # To get the display name:
    >>> # https://docs.djangoproject.com/en/3.0/ref/models/instances/#django.db.models.Model.get_FOO_display
    >>> my_animal_type.get_animal_type_display()
    'Antelope'
    >>> my_animal.get_animal_type_display()
    'Antelope'
  2. @JackAtOmenApps JackAtOmenApps created this gist May 31, 2020.
    41 changes: 41 additions & 0 deletions django_choices.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    from django.db import models

    class Animal(models.Model):

    class AnimalType(models.TextChoices):
    ANTELOPE = 'A', 'Antelope'
    BAT = 'B', 'Bat'
    COUGAR = 'C', 'Cougar'

    animal_type = models.CharField(max_length=1, choices=AnimalType.choices, default=AnimalType.ANTELOPE)

    >>> # Working with the AnimalType class itself
    >>> some_animal_type = Animal.AnimalType
    >>>
    >>> some_animal_type.values
    ['A', 'B', 'C']
    >>>
    >>> some_animal_type.names
    ['ANTELOPE', 'BAT', 'COUGAR']
    >>>
    >>> some_animal_type.labels
    ['Antelope', 'Bat', 'Cougar']
    >>>
    >>> some_animal_type.choices
    [('A', 'Antelope'), ('B', 'Bat'), ('C', 'Cougar')]
    >>>
    >>> dict(some_animal_type.choices)
    {'A': 'Antelope', 'B': 'Bat', 'C': 'Cougar'}
    >>>
    >>> # Working with AnimalType in a Model Instance
    >>> my_animal = Animal()
    >>> my_animal.save()
    >>>
    >>> my_animal_type = my_animal.animal_type
    >>>
    >>> my_animal.animal_type
    'A'
    >>>
    >>> # https://docs.djangoproject.com/en/3.0/ref/models/instances/#django.db.models.Model.get_FOO_display
    >>> my_animal_type.get_animal_type_display()
    'Antelope'