# Get Choice values for question

**URL:** <https://forum.djangoproject.com/t/get-choice-values-for-question/15671>\
**Category:** Using the ORM\
**Created:** [August 29, 2022, 8:49am UTC](https://forum.djangoproject.com/t/get-choice-values-for-question/15671 "2022-08-29T08:49:18Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [August 29, 2022, 8:49am UTC](https://forum.djangoproject.com/t/get-choice-values-for-question/15671/1 "2022-08-29T08:49:18Z")

</div>

Hi,

I have a set of different choices for a question, where each choice has a specific choice value. I need to perform some calculations based on the choice being selected.

I need to get the lowest and highest available value for all the choices a question has. My `choice_value` are

```auto
choice_value_to_score_map = {
    '--': -2,
    '-': -1,
    '0': 0,
    '+': 1,
    '++': 2,
}

```

but not all questions will have the same number of choices and therefore 1 question may have a lower value, i.e. `-1` and the highest value of `0`

I was thinking I would create a new function within the model, but I can’t work out how I get these values cause I am confused about how to get the question id for the question being asked

`choices_values = Choice.objects.filter(question__question_id=1).choice_value` but this throws a error:  
`Related Field got invalid lookup: question_id`

I dont understand why `Choice.objects.filter(question__question_id=1)` doesn’t return the first question

I’ve also tried `ProjectQuestionnaireQuestion.objects.get(pk=1)` but this doesn’t return a value.

Is this because `ProjectQuestionnaireQuestion` has a FK to `Questionnaire`

```auto
class ProjectQuestionnaireQuestion(models.Model):
    questionnaire = models.ForeignKey(ProjectQuestionnaire, on_delete=models.CASCADE)
    sequence = models.IntegerField()
    question = models.TextField()
    description = models.TextField(blank=True)
    heading = models.CharField(max_length=150, blank=True

```

If i was to get this working i could then order by highest/lowest and select first/last - Is this the correct approach?

```auto
    @property
    def value_score(self):
        return self.lowest_value + self.highest_value

```

**Model**

```auto
class Choice(models.Model):
    question = models.ForeignKey(ProjectQuestionnaireQuestion, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200, null=True)
    choice_value = models.CharField(max_length=20, blank=True)
    choice_weight = models.DecimalField( max_digits=5, decimal_places=2,blank=True, null=True)

    def __str__ (self):
        return str(self.choice_text)

    @property
    def raw_score(self):
        if not self.choice_value:
            return 0
        return choice_value_to_score_map.get(self.choice_value, 0)

    @property
    def weighted_score(self):
        return self.raw_score * self.choice_weight

    class Meta:
        verbose_name = "Report Answer Choice" 

choice_value_to_score_map = {
    '--': -2,
    '-': -1,
    '0': 0,
    '+': 1,
    '++': 2,
}

class ProjectQuestionnaireQuestion(models.Model):
    questionnaire = models.ForeignKey(ProjectQuestionnaire, on_delete=models.CASCADE)
    sequence = models.IntegerField()
    question = models.TextField()
    description = models.TextField(blank=True)
    heading = models.CharField(max_length=150, blank=True)
...

```

Thanks

---

<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:** [August 29, 2022, 10:41am UTC](https://forum.djangoproject.com/t/get-choice-values-for-question/15671/2 "2022-08-29T10:41:26Z")

</div>

You’re “reaching too far”.

In `Choice`, `question` _is_ the FK to `ProjectQuestionnaireQuestion`.

So

> [@TommyQuality](#):
>
> Choice.objects.filter(question\_\_question\_id=1).choice\_value

only needs to be:  
`Choice.objects.filter(question_id=1).choice_value`

Or, you could (less efficiently) write this as:  
`Choice.objects.filter(question__id=1).choice_value`

The first is a comparison of the `question` field in `Choice`, the second is a comparison of the `id` field in the `question` object.

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [August 29, 2022, 10:58am UTC](https://forum.djangoproject.com/t/get-choice-values-for-question/15671/3 "2022-08-29T10:58:04Z")

</div>

Thank you, Ken.

I did try this, but it just returns an empty query set.

```auto
In [3]: Choice.objects.filter(question_id=1)
Out[3]: <QuerySet []>

In [4]: Choice.objects.filter(question_id=2)
Out[4]: <QuerySet []>

In [5]: Choice.objects.filter(question_id=3)
Out[5]: <QuerySet []>

```

or

```auto
In [9]: q = Choice.objects.filter(question_id=1).choice_value
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Input In [9], in <cell line: 1>()
----> 1 q = Choice.objects.filter(question_id=1).choice_value

AttributeError: 'QuerySet' object has no attribute 'choice_value'

```

Why is that?

---

<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:** [August 29, 2022, 11:01am UTC](https://forum.djangoproject.com/t/get-choice-values-for-question/15671/4 "2022-08-29T11:01:54Z")

</div>

Issue this query:  
`ProjectQuestionnaireQuestion.objects.all().values('id', flat=True)`  
I’m guessing your entries for this table don’t include pks of 1, 2, or 3.

> [@TommyQuality](#):
>
> `Choice.objects.filter(question_id=1)`

That query returns a queryset - a list of Choice objects - not an individual instance of Choice.  
If you want an attribute of the individual entries in that list, you need to iterate through the list and retrieve the attribute for each entry.

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [August 29, 2022, 11:08am UTC](https://forum.djangoproject.com/t/get-choice-values-for-question/15671/5 "2022-08-29T11:08:35Z")

</div>

> [@KenWhitesell](#):
>
> I’m guessing your entries for this table don’t include pks of 1, 2, or 3.

You’re right. my PKs start higher.

This is handy `ProjectQuestionnaireQuestion.objects.all().values('id')` 🙂

So would i run `q = Choice.objects.filter(question_id=173)` and then iterate through `q` to get the `choice_values`?

---

<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:** [August 29, 2022, 11:10am UTC](https://forum.djangoproject.com/t/get-choice-values-for-question/15671/6 "2022-08-29T11:10:54Z")

</div>

If you need all the individual values, yes.

If you’re just looking to retrieve the highest and lowest values, you could use aggregation over that queryset to retrieve that.

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [August 29, 2022, 11:12am UTC](https://forum.djangoproject.com/t/get-choice-values-for-question/15671/7 "2022-08-29T11:12:13Z")

</div>

ah yeah, I forgot about aggregation. I’ll give that a go.  
Thanks, Ken.

Tom.

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [August 30, 2022, 11:23am UTC](https://forum.djangoproject.com/t/get-choice-values-for-question/15671/8 "2022-08-30T11:23:59Z")

</div>

> [@TommyQuality](#):
>
> `q = Choice.objects.filter(question_id=173)`

Hi, Ken.

Im running `q = Choice.objects.aggregate(Max('choice_weight'),Min('choice_weight'))`

which is returning:

```auto
{'choice_weight__max': Decimal('20.00'),
 'choice_weight__min': Decimal('-8.00')}

```

How do i reference the values so that i can perform a calculation of `choice_weight __max + choice_weight__ min`

i’ve tried `q[0]` but throws an error `KeyError` and if i use `q.keys()` it prints the keys of the dict, but i still can’t reference these.

And is there a way to do this in the original queryset to return the actual result rather than the max/min values?

I’ve looked through the docs, but can’t find the answer that works for me

Thanks

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [August 30, 2022, 12:17pm UTC](https://forum.djangoproject.com/t/get-choice-values-for-question/15671/9 "2022-08-30T12:17:29Z")

</div>

> [@TommyQuality](#):
>
> i’ve tried `q[0]` but throws an error `KeyError` and if i use `q.keys()` it prints the keys of the dict, but i still can’t reference these.

Ignore this. i should have been using q[‘keyName’]

---

<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:** [August 30, 2022, 12:22pm UTC](https://forum.djangoproject.com/t/get-choice-values-for-question/15671/10 "2022-08-30T12:22:32Z")

</div>

> [@TommyQuality](#):
>
> And is there a way to do this in the original queryset to return the actual result rather than the max/min values?

It’s really quite easy - see [Query Expressions | Django documentation | Django](https://docs.djangoproject.com/en/4.1/ref/models/expressions/#query-expressions)

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [August 30, 2022, 2:51pm UTC](https://forum.djangoproject.com/t/get-choice-values-for-question/15671/11 "2022-08-30T14:51:23Z")

</div>

> [@KenWhitesell](#):
>
> It’s really quite easy - see [Query Expressions | Django documentation | Django](https://docs.djangoproject.com/en/4.1/ref/models/expressions/#query-expressions)

Don’t say that! I’m not sure im understanding how this works.  
`q = Choice.objects.aggregate(max_value=Max('choice_weight'),min_value=Min('choice_weight')),F('min_value')*-1+F('max_value')`

On the right lines?

---

<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:** [August 30, 2022, 2:52pm UTC](https://forum.djangoproject.com/t/get-choice-values-for-question/15671/12 "2022-08-30T14:52:30Z")

</div>

You’re close, but the expression would be an annotation added to the query.

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [August 30, 2022, 4:22pm UTC](https://forum.djangoproject.com/t/get-choice-values-for-question/15671/13 "2022-08-30T16:22:25Z")

</div>

Sorry, Ken. Are you saying i should replace the `aggregate` with `annotate` or combine both?  
`q = Choice.objects.annotate(max_value=Max('choice_weight'),min_value=Min('choice_weight')),F('min_value')*-1+F('max_value') `

This returns

```auto
 '...(remaining elements truncated)...']>,
 <CombinedExpression: F(min_value) * Value(-1) + F(max_value)>)

```

---

<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:** [August 30, 2022, 4:23pm UTC](https://forum.djangoproject.com/t/get-choice-values-for-question/15671/14 "2022-08-30T16:23:29Z")

</div>

Before adding this addition, what was your query?

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [August 30, 2022, 4:26pm UTC](https://forum.djangoproject.com/t/get-choice-values-for-question/15671/15 "2022-08-30T16:26:48Z")

</div>

You mean this?  
`q = Choice.objects.aggregate(Max('choice_weight'),Min('choice_weight'))`

---

<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:** [August 30, 2022, 4:28pm UTC](https://forum.djangoproject.com/t/get-choice-values-for-question/15671/16 "2022-08-30T16:28:57Z")

</div>

Yes.

Now, what I wrote was:

> [@KenWhitesell](#):
>
> the expression would be an annotation **added** to the query.

(emphasis added)

Not to remove or replace anything. You **add** an annotation to that query to perform the addition.

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [August 30, 2022, 5:12pm UTC](https://forum.djangoproject.com/t/get-choice-values-for-question/15671/17 "2022-08-30T17:12:19Z")

</div>

> [@TommyQuality](#):
>
> q = Choice.objects.aggregate(Max(‘choice\_weight’),Min(‘choice\_weight’))

I had already tried adding the annotate, but I couldn’t get it to work with the aggregate.

`q = Choice.objects.aggregate(Max('choice_weight'),Min('choice_weight')).annotate(F('min_value')*-1+F('max_value'))`

`'dict' object has no attribute 'annotate'`

---

<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:** [August 30, 2022, 5:24pm UTC](https://forum.djangoproject.com/t/get-choice-values-for-question/15671/18 "2022-08-30T17:24:12Z")

</div>

Sorry, my mistake. I forgot about that aspect of it. Yes, you are right - that aggregation would need to be replaced by an annotation and then they can be combined.

So what you’re looking at (combining everything you’ve identified so far) should probably be closer to:

```auto
q = Choice.objects.filter(question_id=173).annotate(
    max_weight=Max('choice_weight'), 
    min_weight=Min('choice_weight'), 
    weight_range=F('max_weight') - F('min_weight')
)

```

[Edit] ARGH! Yes I reversed them again. That should be `aggregate` in the query above, not `annotate`.

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [August 31, 2022, 1:36pm UTC](https://forum.djangoproject.com/t/get-choice-values-for-question/15671/19 "2022-08-31T13:36:29Z")

</div>

No problem, Ken.

When i run:

```auto
                q = Choice.objects.filter(question_id=133).aggregate(
                    max_weight=Max('choice_weight'), 
                    min_weight=Min('choice_weight'), 
                    weight_range=F('min_weight') *-1 + F('max_weight')
                )
                print(q)

```

it throws this error `AttributeError: 'CombinedExpression' object has no attribute 'name'` which is being caused by ` weight_range=F('min_weight') *-1 + F('max_weight')`

I’m thinking that when using `F` it needs to reference an existing field when in an `aggregate`, but seems to work in `annotate` but I’m trying to avoid returning a queryset

Thanks

---

<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:** [August 31, 2022, 1:55pm UTC](https://forum.djangoproject.com/t/get-choice-values-for-question/15671/20 "2022-08-31T13:55:39Z")

</div>

> [@TommyQuality](#):
>
> `*-1 +`

I’m thinking that it can’t parse that expression as written.

Try changing that to just `-`

e.g.:

> [@KenWhitesell](#):
>
> `weight_range=F('max_weight') - F('min_weight')`

[Next page](https://forum.djangoproject.com/t/get-choice-values-for-question/15671.md?page=2)
