Django:对查询使用.distinct()会导致无效的数据库查询

时间:2010-11-12 14:49:39

标签: django google-app-engine django-models

我想在提交列表中获取不同store_names的列表。

我非常简单的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,Google App Engine)

1 个答案:

答案 0 :(得分:3)

Google Appengine Datastore API不支持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)
相关问题