Create instance of model with field with choices

Kazz

Lets consider this model:

class MyModel(models.Model):
    DEFAULT = 1
    NOT_DEFAULT = 2
    SOMETHING = 3
    SOMETHING_ELSE = 4

    CHOICES = (
        (DEFAULT, 'foo')
        (NOT_DEFAULT, 'bar')
        (SOMETHING, 'tar')
        (SOMETHING_ELSE, 'oof')
    )

    field1 = models.CharField(
        max_length=128,
    )
    field2 = models.SmallIntegerField(
        choices=CHOICES,
        default=DEFAULT
    )

how to i fill it with this data?

name, status
'one', 'foo'
'two', 'oof'
'tree', 'foo'
'four', 'foo'
'five', 'tar'
'six', 'bar'

that does not work since it's expecting an integer

MyModel.objects.create('one','foo')

how do i do it properly without stupid workarounds?

is there a build in mapping like

'foo' -> 1
'oof' -> 4

etc.?

Brown Bear

You have the tuple of tuples CHOICES and you need to find first element by value of the second. So you can generate dict from tuple and get value, for example:

value = 'foo'
{v: k for k, v in MyModel.CHOICES}.get(value)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Create a field in form for each model instance related to another instance in django

From Dev

Django model - changing options in a choices field and migrate

From Dev

Lazy loading a model field's 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

create a model instance with associations

From Dev

create a model instance with associations

From Dev

List polymorphic inheritance choices and create an instance based on that choice

From Dev

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

From Dev

Create instance of model from method

From Dev

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

From Dev

class for model choices

From Dev

Models list as choices in a Model

From Dev

Django model with multiple choices

From Dev

Django: how to get field by field name from Model instance dynamically?

From Dev

save() on a model instance not update the changed field on related model side

From Dev

Create a list of choices automatically

From Dev

How to create a new instance of a model in sailjs?

From Dev

Can't create new instance of django model

From Dev

How create a new instance of a Model that is registered with a Controller?

From Dev

Automatically create instance of a model every day with rails

From Dev

How to create a model instance that has a belongsTo property?

From Dev

php create instance of loaded model in controller

From Dev

ModelForm won't create instance of model

From Dev

Django Multiple Levels Choices for Field

From Dev

Django field choices not properly updating

From Dev

Dynamically set field choices for article

Related Related

  1. 1

    Create a field in form for each model instance related to another instance in django

  2. 2

    Django model - changing options in a choices field and migrate

  3. 3

    Lazy loading a model field's choices

  4. 4

    Changing the choices in Django Model according to a field value

  5. 5

    Convert a Django Model Field Choices into a JSON

  6. 6

    What is Django model field choices best practices?

  7. 7

    Lazy loading a model field's choices

  8. 8

    create a model instance with associations

  9. 9

    create a model instance with associations

  10. 10

    List polymorphic inheritance choices and create an instance based on that choice

  11. 11

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

  12. 12

    Create instance of model from method

  13. 13

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

  14. 14

    class for model choices

  15. 15

    Models list as choices in a Model

  16. 16

    Django model with multiple choices

  17. 17

    Django: how to get field by field name from Model instance dynamically?

  18. 18

    save() on a model instance not update the changed field on related model side

  19. 19

    Create a list of choices automatically

  20. 20

    How to create a new instance of a model in sailjs?

  21. 21

    Can't create new instance of django model

  22. 22

    How create a new instance of a Model that is registered with a Controller?

  23. 23

    Automatically create instance of a model every day with rails

  24. 24

    How to create a model instance that has a belongsTo property?

  25. 25

    php create instance of loaded model in controller

  26. 26

    ModelForm won't create instance of model

  27. 27

    Django Multiple Levels Choices for Field

  28. 28

    Django field choices not properly updating

  29. 29

    Dynamically set field choices for article

HotTag

Archive