class for model choices

Jared Mackey

How can I use a class for choices of a model in Django?

Here is kind of what I want:

class ChoicesCls(object):
    def __init__(self):
        self.OPTION_ONE = 'O1'
        self.OPTION_ONE_SHORT = 'Opt 1'
        self.OPTION_ONE_LONG = 'Option 1'
        self.OPTION_TWO = 'O2'
        self.OPTION_TWO_SHORT = 'Opt 2'
        self.OPTION_TWO_LONG = 'Option 2'

    def get_choices(self):
        return (
            (self.OPTION_ONE, self.OPTION_ONE_LONG),
            (self.OPTION_TWO, self.OPTION_TWO_LONG)
        )


class TestModel(models.Model):
    choices_obj = ChoicesCls()
    field = models.CharField(choices=choices_obj.get_choices)

The main reason I want something like this is so in another place I can do something like

option = "ONE"
getattr(TestModel.choices_obj, "OPTION_{}_SHORT".format(option))
Mad Wombat

Try something like this

class FakeChoices:
    def __init__(self):
        self.OPTION_ONE = 'O1'
        self.OPTION_ONE_SHORT = 'Opt 1'
        self.OPTION_ONE_LONG = 'Option 1'
        self.OPTION_TWO = 'O2'
        self.OPTION_TWO_SHORT = 'Opt 2'
        self.OPTION_TWO_LONG = 'Option 2'

    def __get_tuple(self):
        return (
            (self.OPTION_ONE, self.OPTION_ONE_LONG),
            (self.OPTION_TWO, self.OPTION_TWO_LONG)
        )

    def __getitem__(self, key):
        return self.__get_tuple()[key]

    def __iter__(self):
        return iter(self.__get_tuple())

...

class TestModel(models.Model):
    field = models.CharField(choices=FakeChoices())

As you might imagine you can put all sorts of crazy logic inside FakeChoices class. And you should be able to do

option = "ONE"
getattr(TestModel._meta.get_field('field').choices, "OPTION_{}_SHORT".format(option))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Models list as choices in a Model

From Dev

Django model with multiple choices

From Dev

django use model choices in modelform

From Dev

Django RadioSelect Choices From Model

From Dev

Create instance of model with field with choices

From Dev

Choices defined for model fields not being enforced?

From Dev

Django model - changing options in a choices field and migrate

From Dev

django model choices make no effect on saved data

From Dev

Lazy loading a model field's choices

From Dev

Cannot have multiple model fields with same choices

From Dev

How to have unicode characters in Django model choices?

From Dev

Changing the choices in Django Model according to a field value

From Dev

Convert a Django Model Field Choices into a JSON

From Dev

What is Django model field choices best practices?

From Dev

Lazy loading a model field's choices

From Dev

How to have unicode characters in Django model choices?

From Dev

limit_choices_to within Django model

From Dev

Django Model - Charfield with choices from a row in another model?

From Dev

From UML class diagram to Java : class hierarchy choices

From Dev

Wagtail, how do I populate the choices in a ChoiceBlock from a different model?

From Dev

django Initialising Choices in Form from Class Based View

From Dev

Model Class And POJO Class

From Dev

django - admin model assign value to limit_choices_to from another field inside the model

From Dev

How to Instantiate a model class in another model class?

From Dev

One Django model field as array of strings and another field as dropdown with choices from first field?

From Dev

Populate Django <select> <option>s from model's rows, not its CHOICES

From Dev

Dynamically set choices in django model based on whether adding a new record or editing existing

From Dev

Why to serialize model class

From Dev

Django model derived class

Related Related

  1. 1

    Models list as choices in a Model

  2. 2

    Django model with multiple choices

  3. 3

    django use model choices in modelform

  4. 4

    Django RadioSelect Choices From Model

  5. 5

    Create instance of model with field with choices

  6. 6

    Choices defined for model fields not being enforced?

  7. 7

    Django model - changing options in a choices field and migrate

  8. 8

    django model choices make no effect on saved data

  9. 9

    Lazy loading a model field's choices

  10. 10

    Cannot have multiple model fields with same choices

  11. 11

    How to have unicode characters in Django model choices?

  12. 12

    Changing the choices in Django Model according to a field value

  13. 13

    Convert a Django Model Field Choices into a JSON

  14. 14

    What is Django model field choices best practices?

  15. 15

    Lazy loading a model field's choices

  16. 16

    How to have unicode characters in Django model choices?

  17. 17

    limit_choices_to within Django model

  18. 18

    Django Model - Charfield with choices from a row in another model?

  19. 19

    From UML class diagram to Java : class hierarchy choices

  20. 20

    Wagtail, how do I populate the choices in a ChoiceBlock from a different model?

  21. 21

    django Initialising Choices in Form from Class Based View

  22. 22

    Model Class And POJO Class

  23. 23

    django - admin model assign value to limit_choices_to from another field inside the model

  24. 24

    How to Instantiate a model class in another model class?

  25. 25

    One Django model field as array of strings and another field as dropdown with choices from first field?

  26. 26

    Populate Django <select> <option>s from model's rows, not its CHOICES

  27. 27

    Dynamically set choices in django model based on whether adding a new record or editing existing

  28. 28

    Why to serialize model class

  29. 29

    Django model derived class

HotTag

Archive