从query_set Django获取唯一值

时间:2018-04-14 19:18:28

标签: django django-models django-forms django-views

我试图从s query_set获取唯一结果。

实施例。

ID     |    NOMBRE    |    CLASIFICACION
1           Escoba         Limpieza
2           Trapeador      Limpieza
3           Tornillo       Herramienta

我想得到" Limpieza和Herramienta"只要。目前我正在" Limpieza,Limpieza和Herramineta"

my views.py:

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

my models.py

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

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

context['clasificacion'] = Items.objects.order_by('clasificacion', 'nombre').distinct('clasificacion')

distinct()必须与最左边的order_by()匹配。因此,在distinct() order_by()的第一列// P12 certificate is placed in "assets" folder AssetManager assets = Application.Context.Assets; byte[] bytes; using (StreamReader sr2 = new StreamReader(assets.Open("myCertificate.p12"))) { using (var memstream = new MemoryStream()) { sr2.BaseStream.CopyTo(memstream); bytes = memstream.ToArray(); } } HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.aidap.naimes.faa.gov/aidap/XmlNotamServlet"); X509Certificate2 clientCertificate = new X509Certificate2(bytes, "myPassword"); req.ClientCertificates.Add(clientCertificate); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; string postData = "uid=myUsername&password=myPassword&active=Y"; byte[] byteArray = Encoding.UTF8.GetBytes(postData); req.ContentLength = byteArray.Length; // **Next line triggers the Exception** using (Stream dataStream = req.GetRequestStream()) { dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); } // Show the first 10 lines using (StreamReader sr = new StreamReader(req.GetResponse().GetResponseStream())) { for (int i = 0; i < 10; i++) Console.WriteLine(sr.ReadLine()); } 中使用的列可以使其正常工作。

答案 1 :(得分:0)

请问的回答是否正确,但我认为您希望先执行order_by()操作,以确保获得正确的结果。

image(x,y,data,...)  
data(wrdl_simpl)
plot(wrld_simpl, add = TRUE)