Quantcast
Channel: Como filtar un query_set por valores unicos Django - Stack Overflow en español
Viewing all articles
Browse latest Browse all 2

Como filtar un query_set por valores unicos Django

$
0
0

Estoy tratando de obtener resultados de valores unicos de un query_set en django. Lo que quiero es que me regrese cuantas clasificaciones unicas hay.

Ejemplo. En un Tabla tengo diferentes articulos que se clasifican por tipo

ID     |    NOMBRE    |    CLASIFICACION1           Escoba         Limpieza2           Trapeador      Limpieza3           Tornillo       Herramienta

quiero que me regrese 2 clasificaciones Limpieza y Herramienta. actualmente me regresa las 3 clasificaciones Limpieza, Limpieza y herramienta.

mi vista:

class ItemListView(ListView):    model= Items    def get_context_data(self,**kwargs):        context = super(ItemListView, self).get_context_data(**kwargs)        context['clasificacion'] = Items.objects.order_by('nombre').distinct('clasificacion')        return context

Mi Models

from django.db import models# Create your models here.class Items(models.Model):    nombre = models.CharField(max_length=250)    descripcion = models.CharField(max_length=250)    codigo_proveedor = models.CharField(max_length=250)    clasificacion = models.CharField(max_length=250)    c_minima = models.IntegerField()    c_actual = models.IntegerField()    proveedor = models.ForeignKey('Proveedores',                 on_delete=models.CASCADE)    active = models.BooleanField()        def __str__(self):            return self.nombre +'   -----   '+                   self.clasificacion +'   -----    '+                   str(self.c_actual)class Proveedores(models.Model):    nombre = models.CharField(max_length=250)    telefono = models.CharField(max_length=250)    direccion1 = models.CharField(max_length=250)    direccion2 = models.CharField(max_length=250, null=True)    active =  models.BooleanField()    def __str__(self):        return self.nombre

agradeceria su ayuda


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images