# How to filter only defined number of items in ManyToMany field?

**URL:** <https://forum.djangoproject.com/t/how-to-filter-only-defined-number-of-items-in-manytomany-field/13288>\
**Category:** Using the ORM\
**Created:** [April 21, 2022, 8:52am UTC](https://forum.djangoproject.com/t/how-to-filter-only-defined-number-of-items-in-manytomany-field/13288 "2022-04-21T08:52:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![olof44](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/olof44/32/7858_2.png) [@olof44](https://forum.djangoproject.com/u/olof44)\
**Post date:** [April 21, 2022, 8:52am UTC](https://forum.djangoproject.com/t/how-to-filter-only-defined-number-of-items-in-manytomany-field/13288/1 "2022-04-21T08:52:10Z")

</div>

Hello,  
I have an app with Restaurant model which has type\_of\_food(ManyToMany field) here is the model:

```auto
class Restaurants(models.Model):
    name = models.CharField(max_length=30, unique=True)
    slug = models.SlugField(max_length=30, unique=True)
    user = models.ForeignKey(Account, on_delete=models.CASCADE)
    address = models.CharField(max_length=150)
    website = models.CharField(max_length=100, blank=True, null=True)
    email = models.EmailField()
    phone = models.CharField(max_length=13)
    type_of_food = models.ManyToManyField(TypeOfFood, blank=True)
    available = models.BooleanField(default=False)
    description = models.TextField(blank=True, max_length=1000)
    logo = models.ImageField(upload_to='Restaurant_Logos')
    date_added = models.DateTimeField(auto_now_add=True)

```

So in the Template I want to show only 3 out of many type\_of\_foods that restaurants have.  
I tried to do this in the template:

> {% for type in restaurant.type\_of\_food.all[:2] %}{{ type }}{% endfor %}

But Django template seems to not work with .  
In the template im looping through all the Restaurants, so I can’t make a new object in the views for type\_of\_food seperatly and restrict it to only [:2].  
I’m gonna be really gratefull if someone help me!  
Thank you!

---

<div class="post-metadata">

**Author:** ![plopidou](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/plopidou/32/5148_2.png) [@plopidou](https://forum.djangoproject.com/u/plopidou)\
**Post date:** [April 21, 2022, 9:07am UTC](https://forum.djangoproject.com/t/how-to-filter-only-defined-number-of-items-in-manytomany-field/13288/2 "2022-04-21T09:07:47Z")

</div>

mmm not sure but you could try something like this?

```auto
class Restaurants(models.Model):
    <snip>

    @property
    def type_of_food_first_three(self):
        return self.type_of_food.all()[:2]

```

and in the view, to avoid the extra queries, use `prefetch_related`:

```auto
restaurants = Restaurants.objects.filter(...).prefetch_related(
    'type_of_food'
)

```

and in the template:

```auto
{% for type in restaurant.type_of_food_first_three %}
    {{ type }}
{% endfor %}

```

Haven’t tried to run this, but hopefully that helps you try things in the right direction!

---

<div class="post-metadata">

**Author:** ![olof44](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/olof44/32/7858_2.png) [@olof44](https://forum.djangoproject.com/u/olof44)\
**Post date:** [April 21, 2022, 9:21am UTC](https://forum.djangoproject.com/t/how-to-filter-only-defined-number-of-items-in-manytomany-field/13288/4 "2022-04-21T09:21:58Z")

</div>

It worked, thank you very much!

---

<div class="post-metadata">

**Author:** ![plopidou](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/plopidou/32/5148_2.png) [@plopidou](https://forum.djangoproject.com/u/plopidou)\
**Post date:** [April 21, 2022, 9:23am UTC](https://forum.djangoproject.com/t/how-to-filter-only-defined-number-of-items-in-manytomany-field/13288/5 "2022-04-21T09:23:49Z")

</div>

There might be a better approach with `Prefetch()` (which offers more fine-tuned control over `prefetch`) but hey – look it up 🙂
