[Answer]-Django dynamic model choice modifiable in the web applicaton


Tuple is an Immutable object, and you would not be able to add/remove choices. To achieve what you are looking for, you need a persistent storage. In other words, it as to be stored/retrieved from the database.

What I would recommend is, making this a ForeignKey field, and load the existing choices as an initial fixture into the database.

from django.db import models

class Student(models.Model):
    year_in_school = models.ForeignKey('YearChoices', default= get_default_year())

class YearChoices(models.Model):
    year_in_school = models.CharField(max_length=20)

One way I can think of implementing it would be, in the form, have a CharField with autocomplete choices from YearChoices table , and on POST, if the choice does not exist, create a new entry in the YearChoices table.

Leave a comment