Created
September 12, 2014 00:18
-
-
Save miceno/fddb314144976c4f7356 to your computer and use it in GitHub Desktop.
A dictionary from a list of tuples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# List of selected categories | |
selected_categories = [Category.objects.get(name="TERRESTRE"), Category.objects.get(name="MOLUSCOS")] | |
# Products we want to filter based on the list of selected categories | |
products = Product.objects.filter(category__in = selected_categories) | |
# list is a tuple of tuples like ((id1, product_count1), (id2, product_count2), ... , (idn, product_countn)) | |
qs_count = Category.objects.values(‘id’).filter(product__in=products).annotate(Count('product')) | |
# Get the values | |
list = qs_count.values_list('id', 'product__count') ) | |
# Build the dictionary | |
d = dict( (key,value) for (key,value) in list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment