0

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.

Sardorbek Imomaliev
  • 14,861
  • 2
  • 51
  • 63
Era
  • 171
  • 1
  • 5
  • 13
  • Your question would be clearer if you showed some code. You can use regular forms if model forms do not meet your requirements. – Alasdair Nov 06 '16 at 21:47
  • I added a section of my models to clarify. I haven't come across using regular forms in django yet. Could you point me in the direction of any useful resources? Whenever I've searched for django and forms, I've only got results that use model forms. – Era Nov 06 '16 at 23:04
  • 1
    I would start with the [Django docs](https://docs.djangoproject.com/en/1.10/topics/forms/) – Alasdair Nov 06 '16 at 23:29
  • subcontext1, subcontext2, ... implies incorrect database design – e4c5 Nov 07 '16 at 06:24
  • There are lots of ways to do what you're trying to achieve. It sounds like you will be using a bit of javascript in your template to hide/ show divs etc. Why not just keep track of it there and post the final score back to the view on submit? I mean, you could build a django form that has a checkbox and the text form the above model, but it won't save much work. – Asher Nov 07 '16 at 07:16
  • @e4c5 I know that I should use a many to one field to a subcontext model. I intend to do that in my next iteration. This is my first django project so I'm trying to understand things as I go and improve my code iteratively. My primary concern is to get the main functionality working before improving my model/view structures. But thank you for pointing that out. – Era Nov 07 '16 at 15:52
  • @Asher That sounds like a good solution, my issue is I don't know post data back to a view without a model form. Could you tell me what this process is called so that I can search for it in the documentation? Please excuse the ignorance, this is my first django project and my first backend project to boot. – Era Nov 07 '16 at 15:55

1 Answers1

0

In reply to the above comment, one way to achieve what your trying to do is to include a hidden html field in your form like <input type="hidden" name="result" value="0">. Then in your view you can access it through the request.POST dictionary object.

Check out this from the documentation: https://docs.djangoproject.com/en/1.10/ref/request-response/#django.http.HttpRequest.POST

Also have a look at this questions: Django - taking values from POST request

To make this work you would need to use some javascript code to update the value of the hidden input.

Community
  • 1
  • 1
Asher
  • 677
  • 5
  • 10