 <p>Looking at your code I think you want to show all <code>marcas</code> related to some <code>articulo</code> it via <code>product.type</code>. For this scenario I think you don't need to make any grouping, you can just find all the <code>marcas</code> you are interested in. Here is what i mean:</p> <pre><code>#in the view #add .distinct() if you get duplicates marcas = MarcasOpciones.object.filter(Productos_marca__tipo=articulo.Tipos) #in the template {% for sthelse in marcas %} &lt;li&gt;&lt;a href="#"&gt;{{ sthelse }}&lt;/a&gt;&lt;/li&gt; {% endfor %} </code></pre> <p>There is one possible drawback with this approach - we are not using the <code>|slugify</code> tag to create the match. A possible solution to this is to create <code>slug</code> fields for the both models.</p> <p>However, if that approach doesn't suit you. I advice you to move all the matching logic in the view and again send only the needed <code>marcas</code> in the template. </p> <pre><code>#in the view from django.template.defaultfilters import slugify marcas = [] for sth in productos.all(): if slugify(articulo.Tipos) == slugify(sth.tipo): for sthelse in sth.marca.all(): if sthelse not in marcas: marcas.append(sthelse) #if you need the product that match that marca #you can add it here #sthelse.product = sth </code></pre>