Suppose I have an integer field in a model as follows:
class Foo(models.Model):
class Bar(models.IntegerChoices):
PARROT = 1
MESSIAH = 2
NAUGHTY_BOY = 3
BLACK_KNIGHT = 4
bar = models.IntegerField(choices=Bar.choices, default=Bar.PARROT)
How do I convert the string value to an integer to save it i.e. the same as what appears to happen if I use the field on a "forms.ModelForm" as a drop down? E.g. Take "Parrot" and return 1.
from app.models import Foo
record = get_object_or_404(Foo, id=1)
record.bar = "Parrot"
record.save()
This code gives an error:
Field 'bar' expected a number but got 'Parrot'.