templates - Django regroup using group -
so have this
| element | brand | 1 b 2 c 2 d 1
how do group them brands doesnt repeat, mean dont want show brands twice, want render each brand once. note : brand foreign key
------------------------ edit ---------------------------
class productos(models.model): modelo = models.charfield(max_length=50) tipo = models.foreignkey("tiposopciones", null=true, blank=true, related_name='productos_tipo') marca = models.manytomanyfield(marcasopciones, related_name='productos_marca') i want show marca field once, not repeat it. tried it's repeating it:
{% sth in productos.all %} {% ifequal articulo.tipos|slugify sth.tipo|slugify %} <li> <a href="#"> {% sthelse in sth.marca.all %} {{ sthelse }} {% endfor %} </a> </li> {% endifequal %} {% endfor %} so how should group ?
looking @ code think want show marcas related articulo via product.type. scenario think don't need make grouping, can find marcas interested in. here mean:
#in view #add .distinct() if duplicates marcas = marcasopciones.object.filter(productos_marca__tipo=articulo.tipos) #in template {% sthelse in marcas %} <li><a href="#">{{ sthelse }}</a></li> {% endfor %} there 1 possible drawback approach - not using |slugify tag create match. possible solution create slug fields both models.
however, if approach doesn't suit you. advice move matching logic in view , again send needed marcas in template.
#in view django.template.defaultfilters import slugify marcas = [] sth in productos.all(): if slugify(articulo.tipos) == slugify(sth.tipo): sthelse in sth.marca.all(): if sthelse not in marcas: marcas.append(sthelse) #if need product match marca #you can add here #sthelse.product = sth
Comments
Post a Comment