开发者

Django: Using .distinct() on a query causes invalid DB query

I am trying to get the list of distinct store_names in the list of submissions.

My very simple Django model:

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)  

If I do:

stores = Submission.objects.values_list('store_name', flat=True)

Then the printed result is fine:

[u'amazon.com', u'amazon.com', u'amazon.com', u'buy.com']

However - if I add .distinct() to the query, then I get This query is not supported by the database.

Any ideas on why this would be happening? I've played around with using values instead of valueslist with no luck.

(Latest django release, Python 2.6, OS X, Google App Engine)


Google Appengine Datastore API doen't support distinct functionality. That's what the error you get says, so you can't do it.

All you can do is to filter not unique questions after you fetch the results like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜