Thanks, Ken. Ill come back to the queryset
once I can render the page.
I’ve renamed the models to include Project
prefix and now i have broken it. I’m struggling to identify where i’ve broken it
Traceback (most recent call last):
File "d:\_AzDo\Python.WebApp\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "d:\_AzDo\Python.WebApp\venv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\_AzDo\Python.WebApp\app\views.py", line 425, in get_questionnaire
new_answers = ProjectQuestionnaireQuestion.objects.filter(
File "d:\_AzDo\Python.WebApp\venv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "d:\_AzDo\Python.WebApp\venv\lib\site-packages\django\db\models\query.py", line 974, in filter
return self._filter_or_exclude(False, args, kwargs)
File "d:\_AzDo\Python.WebApp\venv\lib\site-packages\django\db\models\query.py", line 992, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, args, kwargs)
File "d:\_AzDo\Python.WebApp\venv\lib\site-packages\django\db\models\query.py", line 999, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "d:\_AzDo\Python.WebApp\venv\lib\site-packages\django\db\models\sql\query.py", line 1375, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "d:\_AzDo\Python.WebApp\venv\lib\site-packages\django\db\models\sql\query.py", line 1396, in _add_q
child_clause, needed_inner = self.build_filter(
File "d:\_AzDo\Python.WebApp\venv\lib\site-packages\django\db\models\sql\query.py", line 1302, in build_filter
self.check_related_objects(join_info.final_field, value, join_info.opts)
File "d:\_AzDo\Python.WebApp\venv\lib\site-packages\django\db\models\sql\query.py", line 1134, in check_related_objects
self.check_query_object_type(value, opts, field)
File "d:\_AzDo\Python.WebApp\venv\lib\site-packages\django\db\models\sql\query.py", line 1115, in check_query_object_type
raise ValueError(
ValueError: Cannot query "ProjectQuestionnaireResponse object (1)": Must be "ProjectQuestionnaire" instance.
def get_questionnaire(request, project_id, questionnaire_id):
# Get or Create the Response object for the parameters
next = request.POST.get('next', '/')
response, created = ProjectQuestionnaireResponse.objects.get_or_create(
project_name_id=project_id, questionnaire_id=questionnaire_id
)
AnswerFormSet = modelformset_factory(ProjectQuestionnaireAnswer, form=AnswerForm, fields=('answer','notes',), extra=0)
answer_queryset = ProjectQuestionnaireAnswer.objects.filter(response=response
).order_by('question__sequence'
).select_related('question')
if request.method == 'POST':
formset = AnswerFormSet(request.POST)
instances = formset.save()
return HttpResponseRedirect(next)
else:
# Get the list of questions for which no Answer exists
new_answers = ProjectQuestionnaireQuestion.objects.filter(
questionnaire__response=response
).exclude(
answer__response=response
)
# This is safe to execute every time. If all answers exist, nothing happens
for new_answer in new_answers:
ProjectQuestionnaireAnswer(question=new_answer, response=response).save()
answer_formset = AnswerFormSet(queryset=answer_queryset)
return render(request, 'project_questionnaire.html', {'formset': answer_formset,'project_name':response.project_name,'title':response.questionnaire.title})