# combine queryset with array in views ?

**URL:** <https://forum.djangoproject.com/t/combine-queryset-with-array-in-views/20630>\
**Category:** Using the ORM\
**Created:** [April 30, 2023, 10:31am UTC](https://forum.djangoproject.com/t/combine-queryset-with-array-in-views/20630 "2023-04-30T10:31:12Z")\
**Posts on this page:** 1\
**Showing post:** 9

<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:** [May 1, 2023, 12:23pm UTC](https://forum.djangoproject.com/t/combine-queryset-with-array-in-views/20630/9 "2023-05-01T12:23:10Z")

</div>

If you run _only_ this part in the Django shell:

> [@doc1977](#):
>
> ```auto
> duration = Foreign_Missions.objects.filter(employee=OuterRef('id')).annotate(
> duration_days=ExpressionWrapper(
> (F('end') - F('begin')), output_field=fields.DurationField()
> )
> ).order_by().values('duration_days')
> 
> ```

by replacing the `OuterRef` function with any one `Employee` id with more than one related `Foreign_Mission`, what is the content of `duration`?

Then what happens if you run:

> [@doc1977](#):
>
> ```auto
> durations = duration.annotate(total=Sum('duration_days')).values('total')
> 
> ```

What is `durations` after this? Is it a list with multiple elements, but all having the same value for `total`?

If it’s what I think it is, you may be able to replace that with:  
`durations = duration.annotate(total=Sum('duration_days')).values('total')[:1]`  
to ensure that only one value is returned.

You can find many examples of that being used in Subquery expressions here in the forum. You can do a search on `"[:1]"` to find them. A couple of recent examples include:

- [how to filter the inbox messages that belong to a user?](https://forum.djangoproject.com/t/how-to-filter-the-inbox-messages-that-belong-to-a-user/18887)
- [How to order\_by ForeignKey that matches query?](https://forum.djangoproject.com/t/how-to-order-by-foreignkey-that-matches-query/17414)

---

_[View the full topic](https://forum.djangoproject.com/t/combine-queryset-with-array-in-views/20630)._
