In my django app I am trying to generate a question from a set of objects, then when that is answered create a result object that has a score based on how much of the answer the user got correct.
I have split up my set of question objects into the appropriate parts to iterate through to generate a form. What I would like to know is how to calculate the total number of checkboxes on a page and the number of check boxes selected and pass that to django so I can calculate and set the score attribute of a result object in a view.
From what I have seen of ModelForms, they only work by directly inputting data into the database rather than submitting it to the backend for further computation. Furthermore it seems that the ModelForm is generated from the model type that you want to create, whereas I want to create a result as a result of a form generated from another model.
Here are the result and concept models to demonstrate my query:
class Concept(Chunk):
application = models.CharField(max_length=500)
subconcept1 = models.CharField(max_length=500, blank=True, null=True)
subconcept2 = models.CharField(max_length=500, blank=True, null=True)
subconcept3 = models.CharField(max_length=500, blank=True, null=True)
subconcept4 = models.CharField(max_length=500, blank=True, null=True)
subconcept5 = models.CharField(max_length=500, blank=True, null=True)
subconcept6 = models.CharField(max_length=500, blank=True, null=True)
subconcept7 = models.CharField(max_length=500, blank=True, null=True)
subconcept8 = models.CharField(max_length=500, blank=True, null=True)
subconcept9 = models.CharField(max_length=500, blank=True, null=True)
subconcept10 = models.CharField(max_length=500, blank=True, null=True)
conceptimage = models.FileField(blank=True, null=True)
@property
def mode(self):
return "concept"
class Result(models.Model):
rel_chunk = models.ForeignKey(Chunk, on_delete=models.CASCADE)
score = models.IntegerField()
timestamp = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(Profile, on_delete=models.CASCADE)
class Meta:
get_latest_by = 'timestamp'
In the template, for this example there would be a question about a specific concept. A button to get the answer would hide the question div and put the answer div in its place. The answer div iterates through the concept showing each subconcept and a checkbox next to it. Then the user hits a button to submit this, and on the submit I want to get the user input to calculate the score of the result object.