irfanpule / django-form-surveys

Django form survey is an application Django to easier create form survey and easy integrated for your project.
MIT License
56 stars 19 forks source link

Question, not really an issue. #67

Closed lmaloney closed 9 months ago

lmaloney commented 9 months ago

I would like to store an identifier with each saved copy of the survey results. The identifier is to be used to associate the survey results with the person taking the survey.

What is the recommend way to do that?

irfanpule commented 9 months ago

Hi @lmaloney

Actually to associate all record you can use id but if you need identifier for easy read, I think you can use survey slug and Question key

https://github.com/irfanpule/django-form-surveys/blob/4cbe5bd45377a886b76368943b82453ece524045/djf_surveys/models.py#L51 https://github.com/irfanpule/django-form-surveys/blob/4cbe5bd45377a886b76368943b82453ece524045/djf_surveys/models.py#L87

This solution can't track all record. Some case you still use id to get data.

lmaloney commented 9 months ago

So does the id refer to all questions for a survey.. slug?

Are you saying that each survey, has a standard Django slug.. and each stored, response for a completed survey then uses the standard id for that survey slug?

If that's the case, then I think I'll need to either pass in the identifier of the survey 'respondent', OR... I'll need to get back that ID once the survey is posted.. from the context of the view for the respondent.

Where would I extended the successful post of a survey?

irfanpule commented 9 months ago

You can see model Answer

https://github.com/irfanpule/django-form-surveys/blob/4cbe5bd45377a886b76368943b82453ece524045/djf_surveys/models.py#L144

at this model you can track question, user_answer (respondent), value. So you can track survey from question field in model Answer. For example if you want to access Survey from Answer

answer = Answer.objects.get(id=1)

# to get survey name
answer.question.survey.name

# to get question
answer.question.label

# to get user
anser.user_answer.user.username

in other case, if you want to show all answer from survey. For example

survey = Survey.objects.get(slug=“test-survey”)

# filter by survey
answers = Answer.objects.filter(question__survey=survey)

# loop data
for answer in answers:
    print(answer.question.label, answer.value)
lmaloney commented 9 months ago

oh wow, that's nice!

This is much better.. then what I did!

i edited djf_models.py to include MY model, then I edited djf_views.py to copy the data into my table, during the form post. (cringe)

Your's is the CORRECT way. I'll refactor.