Django: Create form from items in model, where each item in the model has multiple associated questions
I want to ask users multiple questions for each of a list of items in a Django model.
As an illustrative example: Suppose I'm collecting data on people's opinions of the Harry Potter films. I want to know if someone has a) watched one of the films, and b) what rating (out of 10) they give said film.
Assume that my Django app contains 3 models:
- Films - to populate the list of films;
- Users - details on the person who has responded;
- Reponses - responses for each user. This contains three columns: UserID, FilmID, Rating. It is only populated when a user has given a score for a film (i.e., no blank ratings)
The table below shows what kind of answer I'd want to receive: a check of whether they watched the film, and their rating out of 10.
| Film | Watched? | Rating |
|---|---|---|
| HP-1 | Y | 8 |
| HP-2 | Y | 9 |
| HP-3 | N | - |
| HP-4 | N | - |
| HP-5 | N | - |
| HP-6 | Y | 7 |
| HP-7 | N | - |
| HP-8 | N | - |
The HTML of the form would have a similar layout: film name, check box for if it has been watched, and the rating field (dropdown or integer field).
This would then load the following rows into the Responses table:
| UserID | FilmID | Rating |
|---|---|---|
| 1 | HP-1 | 8 |
| 1 | HP-2 | 9 |
| 1 | HP-6 | 7 |
What would be the best approach for creating such a form using Django?
I've had a look into formsets and similar methods as well as this blog on inline formsets, but can't seem to piece it together to fit what I'm after (my knowledge of Django is limited, mind).
I may be looking down the complete wrong route, so any help would be appreciated. Thanks.