# filter in the django manytomany field

**URL:** <https://forum.djangoproject.com/t/filter-in-the-django-manytomany-field/33013>\
**Category:** Templates & Frontend\
**Created:** [July 16, 2024, 6:40am UTC](https://forum.djangoproject.com/t/filter-in-the-django-manytomany-field/33013 "2024-07-16T06:40:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rdeveloperswork](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rdeveloperswork/32/22637_2.png) [@rdeveloperswork](https://forum.djangoproject.com/u/rdeveloperswork)\
**Post date:** [July 16, 2024, 6:40am UTC](https://forum.djangoproject.com/t/filter-in-the-django-manytomany-field/33013/1 "2024-07-16T06:40:15Z")

</div>

In the `College` model, I have a field named `courses_info` which contains multiple entries. There are a total of 114 courses available. Each college offers different courses. For example, let’s consider that College1 offers 10 courses, including BSc, MSc, and 8 other courses. Please write a Django ORM query to filter these courses.

---

<div class="post-metadata">

**Author:** ![anefta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anefta/32/10649_2.png) [@anefta](https://forum.djangoproject.com/u/anefta)\
**Post date:** [July 29, 2024, 2:55pm UTC](https://forum.djangoproject.com/t/filter-in-the-django-manytomany-field/33013/2 "2024-07-29T14:55:51Z")

</div>

Show your models.py, your view.py and your templates.

Most probably something like that would work:

```auto
from django.db import models

class Course(models.Model):
    name = models.CharField(max_length=200)

class College(models.Model):
    name = models.CharField(max_length=200)
    courses = models.ManyToManyField(Course, related_name='colleges')

```

Now, let’s say you want to get all the courses offered by a specific college. You can do this with the following query:

```auto
college = College.objects.get(name='College1')
courses = college.courses.all()

```

This will return a QuerySet of Course objects that are related to the College object with the name ‘College1’.

If you want to filter the courses based on some criteria, you can chain additional filter conditions. For example, to get only the courses with names ‘BSc’ or ‘MSc’, you can do:

```auto
courses = college.courses.filter(name__in=['BSc', 'MSc'])

```
