# Queryset item as var in another queryset

**URL:** <https://forum.djangoproject.com/t/queryset-item-as-var-in-another-queryset/8512>\
**Category:** Using Django\
**Created:** [June 25, 2021, 2:30pm UTC](https://forum.djangoproject.com/t/queryset-item-as-var-in-another-queryset/8512 "2021-06-25T14:30:15Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![notrednamc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/notrednamc/32/2812_2.png) [@notrednamc](https://forum.djangoproject.com/u/notrednamc)\
**Post date:** [June 25, 2021, 2:30pm UTC](https://forum.djangoproject.com/t/queryset-item-as-var-in-another-queryset/8512/1 "2021-06-25T14:30:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 25, 2021, 2:38pm UTC](https://forum.djangoproject.com/t/queryset-item-as-var-in-another-queryset/8512/2 "2021-06-25T14:38:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![notrednamc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/notrednamc/32/2812_2.png) [@notrednamc](https://forum.djangoproject.com/u/notrednamc)\
**Post date:** [June 25, 2021, 2:48pm UTC](https://forum.djangoproject.com/t/queryset-item-as-var-in-another-queryset/8512/3 "2021-06-25T14:48:41Z")

</div>

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

views.py

```auto
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

```auto
{% 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 %}

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 25, 2021, 2:54pm UTC](https://forum.djangoproject.com/t/queryset-item-as-var-in-another-queryset/8512/4 "2021-06-25T14:54:26Z")

</div>

> [@notrednamc](#):
>
> ```auto
> data = {
> 'sitetitle': 'CAT - SSP',
> 'response': response,
> 'assessment': assessment,
> 'customer': customer,
> 'response': response,
> 'response_list': response_list,
> }
> 
> ```

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?)

---

<div class="post-metadata">

**Author:** ![notrednamc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/notrednamc/32/2812_2.png) [@notrednamc](https://forum.djangoproject.com/u/notrednamc)\
**Post date:** [June 25, 2021, 3:19pm UTC](https://forum.djangoproject.com/t/queryset-item-as-var-in-another-queryset/8512/5 "2021-06-25T15:19:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 25, 2021, 3:57pm UTC](https://forum.djangoproject.com/t/queryset-item-as-var-in-another-queryset/8512/6 "2021-06-25T15:57:47Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![notrednamc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/notrednamc/32/2812_2.png) [@notrednamc](https://forum.djangoproject.com/u/notrednamc)\
**Post date:** [June 25, 2021, 4:46pm UTC](https://forum.djangoproject.com/t/queryset-item-as-var-in-another-queryset/8512/7 "2021-06-25T16:46:04Z")

</div>

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\>

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 25, 2021, 5:13pm UTC](https://forum.djangoproject.com/t/queryset-item-as-var-in-another-queryset/8512/8 "2021-06-25T17:13:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![notrednamc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/notrednamc/32/2812_2.png) [@notrednamc](https://forum.djangoproject.com/u/notrednamc)\
**Post date:** [June 25, 2021, 5:18pm UTC](https://forum.djangoproject.com/t/queryset-item-as-var-in-another-queryset/8512/9 "2021-06-25T17:18:32Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 25, 2021, 5:22pm UTC](https://forum.djangoproject.com/t/queryset-item-as-var-in-another-queryset/8512/10 "2021-06-25T17:22:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![notrednamc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/notrednamc/32/2812_2.png) [@notrednamc](https://forum.djangoproject.com/u/notrednamc)\
**Post date:** [June 25, 2021, 5:44pm UTC](https://forum.djangoproject.com/t/queryset-item-as-var-in-another-queryset/8512/11 "2021-06-25T17:44:50Z")

</div>

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