在django中选择DISTINCT个别列? [英] Select DISTINCT individual columns in django?

查看:374
本文介绍了在django中选择DISTINCT个别列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有任何方法可以在Django中进行查询,那么这不是一个 SELECT * FROM ... ,我很好奇。我试图做一个 SELECT DISTINCT columnName FROM ...



具体来说我有一个模型看起来像:

  class ProductOrder(models.Model):
Product = models.CharField(max_length = 20,promary_key = True)
Category = models.CharField(max_length = 30)
Rank = models.IntegerField()
pre>



其中排名分类。我想能够遍历所有类别,在该类别中的每个排名上进行一些操作。



我想先列出所有系统中的类别,然后查询该类别中的所有产品,并重复执行,直到每个类别都被处理。



我宁愿避免生成SQL,但如果我必须去那里会好的虽然我从来没有在Django / Python中编写原始SQL。

解决方案

一种方法来获取不同列名的列表数据库将使用 distinct() values()



在您的情况下,您可以执行以下操作来获取不同类别的名称:

  q = ProductOrder.objects.values('Category')。distinct()
print q.query#See为自己。

#查询会像
#SELECT DISTINCTapp_productorder。categoryFROMapp_productorder

这里有几件事要记住。首先,这将返回一个 ValuesQuerySet ,其行为与 QuerySet 不同。当您访问说, q (上述)的第一个元素,您将得到一个 字典 ,而不是一个实例的 ProductOrder



其次,阅读 distinct()的文档中的/#distinctrel =noreferrer>警告注释。上述示例将工作,但$ code> distinct()和 values()的所有组合可能不会。



PS :对于字段,使用 小写字母名称 是个好主意在一个模型。在这种情况下,这将意味着重写您的模型,如下所示:

  class ProductOrder(models.Model):
product = models.CharField(max_length = 20,primary_key = True)
category = models.CharField(max_length = 30)
rank = models.IntegerField()
/ pre>

I'm curious if there's any way to do a query in Django that's not a "SELECT * FROM..." underneath. I'm trying to do a "SELECT DISTINCT columnName FROM ..." instead.

Specifically I have a model that looks like:

class ProductOrder(models.Model):
   Product  = models.CharField(max_length=20, promary_key=True)
   Category = models.CharField(max_length=30)
   Rank = models.IntegerField()

where the Rank is a rank within a Category. I'd like to be able to iterate over all the Categories doing some operation on each rank within that category.

I'd like to first get a list of all the categories in the system and then query for all products in that category and repeat until every category is processed.

I'd rather avoid raw SQL, but if I have to go there, that'd be fine. Though I've never coded raw SQL in Django/Python before.

解决方案

One way to get the list of distinct column names from the database is to use distinct() in conjunction with values().

In your case you can do the following to get the names of distinct categories:

q = ProductOrder.objects.values('Category').distinct()
print q.query # See for yourself.

# The query would look something like
# SELECT DISTINCT "app_productorder"."category" FROM "app_productorder"

There are a couple of things to remember here. First, this will return a ValuesQuerySet which behaves differently from a QuerySet. When you access say, the first element of q (above) you'll get a dictionary, NOT an instance of ProductOrder.

Second, it would be a good idea to read the warning note in the docs about using distinct(). The above example will work but all combinations of distinct() and values() may not.

PS: it is a good idea to use lower case names for fields in a model. In your case this would mean rewriting your model as shown below:

class ProductOrder(models.Model):
    product  = models.CharField(max_length=20, primary_key=True)
    category = models.CharField(max_length=30)
    rank = models.IntegerField()

这篇关于在django中选择DISTINCT个别列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆