[Answered ]-"ValidatioError: Value … is not a valid choice" in Django even with a valid choice

1👍

Since this is a CharField, the keys should be strings, so '0', not 0:

OBJECT_TYPES = (
    ('0', "analog-input"),
    ('1', "analog-output"),
    ('2', "analog-value")
)

an alternative is to work with an IntegerField:

OBJECT_TYPES = (
    (0, "analog-input"),
    (1, "analog-output"),
    (2, "analog-value")
)

class MyModel(models.Model):
    object_type = models.IntegerField(
        choices=OBJECT_TYPES,
        blank=True,
        null=True
    )

Leave a comment