1π
β
I think the problem is that you were defining choice_field
as CharField
, but your choices are using id
as keys, which are integers. You need to change the choice_field
to be models.IntegerField
.
Also, sounds like your choice_field
is based on another modelβs entries. Your way of populating data might not work because your CHOICES
variable would get evaluated only once. You should use ForeignKey
for choice_field
instead:
class Model2(models.Model):
choice_field = models.ForeignKey(Model1)
If you use django default way of rendering form, ForeignKey
would be rendered as a dropdown just like choices
, so you should definitely use that.
π€Shang Wang
Source:stackexchange.com