2010-11-12 100 views
2

我试图在提交列表中获取不同store_names的列表。Django:在查询中使用.distinct()会导致无效的数据库查询

我很简单的Django模型:

class Submission(models.Model): 
    title = models.CharField(max_length=50, null=True, blank=True) 
    description = models.CharField(max_length=200, null= True, blank=True) 
    store_name = models.CharField(max_length=200) 

如果我做的:
stores = Submission.objects.values_list('store_name', flat=True)
然后打印的结果是罚款:
[u'amazon.com', u'amazon.com', u'amazon.com', u'buy.com']

但是 - 如果我添加.distinct()到查询,然后我得到该查询不支持数据库。

关于为什么会发生这种情况的任何想法?我玩过使用而不是valueslist没有运气。

(最新发布的Django,Python 2.6中,OS X,谷歌App引擎)

+0

为了减轻这个问题了一点,你可以用'打印stores.query'和发布原始SQL输出这里! – 2010-11-12 15:21:42

+0

SELECT DISTINCT app_submission.store_name FROM app_submission – Eitan 2010-11-12 19:15:42

回答

3

谷歌的AppEngine数据存储API doen't支持distinct功能。这就是你所说的错误,所以你不能这样做。

所有你能做的就是你获取结果这样的后过滤不是唯一的问题:

stores = Submission.objects.values_list('store_name', flat=True) 
unique_stores = [] 
for store in stores: 
    if store not in unique_stores: 
     unique_stores.append(store) 
+0

谢谢,这是一个解决问题的非常实用的方法! – Eitan 2010-11-12 19:16:47

+0

它看起来像GAE现在支持DISTINCT https://developers.google.com/appengine/docs/python/datastore/gqlreference#Syntax – 2013-02-04 07:54:08