I have a set of models which are similar logically to the ones below. The models are the structure of a form with groups of elements that we can add to and change as required. The elements are HTML form elements like radio buttons, checkboxes and textareas that a user can fill out.
class Form(models.Model):
name = models.CharField(max_length=50)
class FormGroup(models.Model):
name = models.CharField(max_length=50)
form = models.ForeignKey('forms.Form', related_name="groups")
class FormGroupElements(models.Model):
name = models.CharField(max_length=50)
type = models.CharField(max_length=50, choices=['checkbox', 'radio', 'textarea'])
group = models.ForeignKey('forms.FormGroup', related_name="elements")
For this example, we are working on a "Dining Survey" with just the Entree section.
When serialized, it looks like this.
{
'form-id': '6be0ac8,
'name': "Entree Survey",
'groups':[
{
'id': '9cba9e2',
'name': "I enjoyed my entree",
'elements':[
{
'id': '009c8a2',
'name': "Yes",
'type': "radio",
},
{
'id': 'c02da91',
'name': "No",
'type': "radio",
}
]
},
{
'id': '0f91c10',
'name': "The meal I had for my entree was",
'elements':[
{
'id': 3,
'name': "Meal",
'type': "textarea",
},
]
},
]
}
This renders out into HTML which looks like this:
<input type="radio" name="9cba9e2-i-enjoyed-my-entree" value="Yes" />
<input type="radio" name="9cba9e2-i-enjoyed-my-entree" value="No" />
<input type="text" name="0f91c10-the-meal-i-had-for-my-entree-was" value="" />
I would like to save their response in a format that allows them to review and change their responses. For example, if they end up having a lunch, we would add the lunch form into their profile. Subsequently if they have dinner, we would add the dinner survey into their Dining Survey with us.
We would like to keep versions of their dining survey with us so that we can keep track of their experience through time. We would also like to show them their previous responses to the elements of the forms they have already filled out. For example, when the client comes back and fills out the lunch form, they can see their previous responses and overwrite them. They should also be able to see any new fields we may have added to the forms they have previously filled out.
In my own futile attempt to solve this problem I have created the following models:
class DiningSurvey(models.Model):
client = models.ForeignKey('clients.Client', related_name="dining_surveys")
elements = models.ManyToManyField('form.FormGroupElements', related_name='surveys', through="form.SurveyValues")
class SurveyValues(models.Model):
survey = models.ForeignKey('forms.DiningSurvey', related_name="values")
element = models.ForeignKey('forms.FormGroupElements', related_name="values")
value = models.TextField()
And by pure willpower (read bad coding) I have made this work. This solution feels really clunky to me and I am sure there is a better way.
I hope that you guys are able to point me in the right direction because I would not know what this design pattern is even called!
Please note that this dining survey is only an example, I understand for this use case this is not 100% applicable however in the real use case this is the best solution for us.
Aucun commentaire:
Enregistrer un commentaire