# Django - regrouping and filtering

**URL:** <https://forum.djangoproject.com/t/django-regrouping-and-filtering/27311>\
**Category:** Templates & Frontend\
**Created:** [January 23, 2024, 1:59pm UTC](https://forum.djangoproject.com/t/django-regrouping-and-filtering/27311 "2024-01-23T13:59:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![makeitwork](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/makeitwork/32/15512_2.png) [@makeitwork](https://forum.djangoproject.com/u/makeitwork)\
**Post date:** [January 23, 2024, 1:59pm UTC](https://forum.djangoproject.com/t/django-regrouping-and-filtering/27311/1 "2024-01-23T13:59:24Z")

</div>

**model**

```auto
class costdata(models.Model):
	date = models.DateField()
	type = models.CharField(choices=expense_choice,max_length=250)
	expense = models.CharField(max_length=250)
	amount = models.FloatField(default=0.0)

```

**views**

```auto
costdata = costdata.objects.values('type','expense','date__month').filter(date__range=[fromdat,todat]).annotate(total = Sum('amount'))

```

In template I need to show data related to type=‘something’. How can I filter that in regroup?

`{% regroup costdata|dictsort:"expense" by expense as expense_list %}`

No clue what to do after this? How can I filter with type=''something"?

---

<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:** [January 23, 2024, 2:06pm UTC](https://forum.djangoproject.com/t/django-regrouping-and-filtering/27311/2 "2024-01-23T14:06:26Z")

</div>

You don’t do that in the template. You change your view to filter the data before it is rendered.

---

<div class="post-metadata">

**Author:** ![makeitwork](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/makeitwork/32/15512_2.png) [@makeitwork](https://forum.djangoproject.com/u/makeitwork)\
**Post date:** [January 23, 2024, 2:14pm UTC](https://forum.djangoproject.com/t/django-regrouping-and-filtering/27311/3 "2024-01-23T14:14:01Z")

</div>

Tried this:

But no idea where its wrong.

```auto
{% regroup costdata|dictsort:"expense" by expense as expense_list %}

{% for k,v in expense_list %}
{% for data in v %}
{% if data.type == 'cm2' %}
{% regroup data by data.expense as el %}
{% for a in el %}
{{a.grouper}}<br>
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}

```

Here the result is None.
