Queryset item as var in another queryset

Hello All,

On my template I have 2 query sets (assessment and response), I am looping though assessment to display items. I need to display items from response from within the assessment outer loop. I would like to use one of the assessment items (assessment.practice) as the attribute in the inner result loop result.(assessment.practice) Below is what I have tried

  1. result.assessment.practice
  • No output
  1. with assessment.practice as prac
    result.prac
  • prac contains value but there is not result when used with result.prac
  1. def get(o, index):
    return o[index]
    result|get:assessment.practice
  • string indices must be integers

Is there a way to do this or do I need to rethink it.

I think we’re going to see more specific information about what you’re trying to do here. It’s going to be most helpful if you would post your view and template for us to understand the relationships between them and how you’re trying to use them.

View and template code below, I hope everything copied okay.

views.py

def AssessmentListView(request):
    
	if request.POST.get("document_btn"):
	    
	    # response = helpers.GetSingleAssessment(framework_filter, maturity_level, request.POST)
	    template = 'backend/assessment_output.html'
	    assessment = CMMC.objects.filter(maturity_level=maturity_level).order_by('id')
	    customer = Customers.objects.filter(id=customer_id)
	    response = helpers.GetSingleAssessment(framework_filter, maturity_level, request.POST)
	    
	    data = {
		'sitetitle': 'CAT - SSP',
		'response': response,
		'assessment': assessment,
		'customer': customer,
		'response': response,
		'response_list': response_list,
	    }
	    
	    return render(request, template, data)

template

{% for item in assessment %}
<tr>
<td style="border: 1px solid #4aa0e0;" colspan="2" valign="top" bgcolor="#c2dff4" width="646">
<p style="margin-left: 0in; page-break-after: avoid;"><strong>{{item.domain}}</strong></p>
</td>
</tr>
<tr valign="top">
<td style="border: 1px solid #4aa0e0;" width="153">
<p style="margin-left: 0in; page-break-after: avoid;"><span style="color: #000000;"><strong><span style="color: #0000ff;">
<span>{{ item.practice_id}} </span></span><span>{{ item.practice }}</span></strong></span></span></p>
                     
{% with item.practice_id|period_replace as prac %}
{% for answer in response %}
{% if answer.prac %}
<p style="margin-left: 0in; page-break-after: avoid;"><span style="color: #000000;">☒<strong><span>PASS </span></strong></span></p>
<p style="margin-left: 0in; page-break-after: avoid;"><span style="color: #000000;">☐<strong><span>FAIL</span></strong></span></p>
{% else %}
<p style="margin-left: 0in; page-break-after: avoid;"><span style="color: #000000;">☐<strong><span>PASS </span></strong></span></p>
<p style="margin-left: 0in; page-break-after: avoid;"><span style="color: #000000;">☒<strong><span>FAIL</span></strong></span></p>
{% endif %}

{% endfor %}
{% endwith %}

{% endfor %}

You’ve got response twice, and response_list isn’t defined in your view.

What’s the relationship between an assessment and a response? (Given an assessment, how do you know what reponses apply to it?)

Sorry! response_list and one of the response should be removed.

The relationship between assessment and response: assessment contains ALL assessment questions and response contains the answers.

NOTE:
I realize there is a much better way to do this, I am learning django as I go and this is a pre alpha prototype that will get a full redesign when I get approval and funding lol

Are you looking to:

  • show all questions with all answers to each?
  • show one question with all answers to that question?
  • show all questions with one answer to each from a specific user?

From a model perspective - what are the fields that link the two tables?
How do you know which responses apply to which question?

I want to show all questions from assessment. Response contains the true/false answers to those questions. The tables are not linked via foreign key but the value of assessment.practice_id, for example, would be “XX1001”. This is the field/column name of the response. In the response the field “XX1001” value would be true/false.

So in my outer (assessment) loop where assessment.practice_id I jump into the inner (response) loop and am trying to use the value of assessment.practice_id to access the equivalent value in the response fields as response.<assessment.practice_id>

Ok, there are a couple things here -

First, I wouldn’t have designed the tables this way. If a response is related to a specific question, then there should be a relationship. What you have is unnormalized data, with the possibility of your tables getting out of sync.

Either, that field holding the “XX1001” value should be an FK to the assessment with the practice_id field of the same value, or there should be a third table consisting only of the numerical PK and the “text_name” (e.g. “XX1001”) and both the assessment and response tables would have FKs to that table.

As it stands here, you can work with your structures as defined, but you’re going to need to perform your association between the two tables in the view and not in the template. For example, you could create a list of associated responses as a new field in your model, and then iterate over that list in your template. What you’re trying to do by making that comparison within the template isn’t the right way to approach this.

Understood. The redesign (with acquired knowledge lol) will address these situations. As it stands I am hoping to get a viable prototype soon, requirement creep for this prototype is what I am dealing with now lol. I might just have to hard code in the meantime. Thanks again Ken!

Understood. Go ahead and do the association in the view then - it’ll work for now, under the assumption that you don’t have any malicious users.

That is a good assumption lol this is strictly internal and behind security.