'Better way to do the django api response
I have tried to create a model to represent a situation like this.
each question row can have a multiple question column. there are multiple types of question column
class QuestionRow(models.Model):
report_question = models.CharField(max_length=200)
report_group = models.CharField(max_length=20)
class QuestionColumn(models.Model):
header_text = models.CharField(max_length=100)
data_type = models.CharField(max_length=10)
class QuestionItem(models.Model):
column = models.ForeignKey(QuestionColumn)
row = models.ForeignKey(QuestionRow)
My objective is to find the optimized way to query and return the response. where each question row in question item may or may not have multiple question columns. thinking of a way to do this using django annotate, aggregate
[{
"report_group": 1,
"question_row": "1a. Alarm system not active or not sufficient?",
"question_columns" : [
{
"header_text": "Yes/No",
"data_type": "Bool"
},
{
"header_text": "Risk Score",
"data_type": ""
}
]
},
{
"report_group": 1,
"question_row": "1b. Notification system not active or inactive?",
"question_columns" : [
{
"header_text": "Yes/No",
"data_type": "Bool"
},
{
"header_text": "Risk Score",
"data_type": ""
}
]
}]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
