Wednesday, 14 August 2013

Django dynamic model choice modifiable in the web applicaton

Django dynamic model choice modifiable in the web applicaton

Suppose I have a model with choice, see below (this is the django doc
example). What I need is that my web application be able to dynamically
add and delete choices and I need my model to get updated with those
choices. In fact, the best way would be to have choices stored in a
separate database and the model to have a relationship to them via
ForeignKey. How do I modify the code so that I will still have selectable
choices in the model? Thanks.
from django.db import models
class Student(models.Model):
YEAR_IN_SCHOOL_CHOICES = [
('FR', 'Freshman'),
('SO', 'Sophomore'),
('JR', 'Junior'),
('SR', 'Senior'),
]
year_in_school = models.CharField(max_length=2,
choices=YEAR_IN_SCHOOL_CHOICES,
default=FRESHMAN)

No comments:

Post a Comment