Say I have an unknown number of questions. For instance:
- Bright blue [y / n]
- What date did you give birth [date]
- What is pi [3.14]
- What is great intelligence [100]
Now, each of these questions represents a different, but type-specific answer (boolean, date, float, int). Naturally, django can happily deal with them in the model.
class SkyModel(models.Model):
question = models.CharField("Is the sky blue")
answer = models.BooleanField(default=False)
class BirthModel(models.Model):
question = models.CharField("What date were your born on")
answer = models.DateTimeField(default=today)
class PiModel(models.Model)
question = models.CharField("What is pi")
answer = models.FloatField()
But this has an obvious problem in that each question has a specific model, so if we need to add a question later, I have to change the database. Ugh. So, now I want to get a fantasy - how to set up a model where, by converting the type of response, it happens automatically?
ANSWER_TYPES = (
('boolean', 'boolean'),
('date', 'date'),
('float', 'float'),
('int', 'int'),
('char', 'char'),
)
class Questions(models.model):
question = models.CharField(()
answer = models.CharField()
answer_type = models.CharField(choices = ANSWER_TYPES)
default = models.CharField()
So, theoretically, this will do the following:
- , ,
.
- , , answer_type. 3.14 float str.
? - ?
!