Skip to content

Instantly share code, notes, and snippets.

@Greymalkin
Last active August 29, 2015 14:26
Show Gist options
  • Save Greymalkin/82e1359996fa7395f4e2 to your computer and use it in GitHub Desktop.
Save Greymalkin/82e1359996fa7395f4e2 to your computer and use it in GitHub Desktop.
HAHAAS: scratch pad
from django.db.models import Count
from django.db.models.sql.where import ExtraWhere, AND
from django.db import reset_queries, connection
>>> categories = '27,18'
>>> filter_string = 'ringtones_bundle_category.category_id IN ({})'.format(categories)
>>> bundle_join_string = 'ringtones_bundlepack.bundle_id = ringtones_bundle.id'
>>> category_join_string = 'ringtones_bundle_category.bundle_id = ringtones_bundle.id'
>>> models.BundlePack.objects.extra(where=[string,'ringtones_bundle_category.bundle_id = ringtones_bundle.id','ringtones_bundlepack.bundle_id = ringtones_bundle.id']).extra(tables=['ringtones_bundle_category','ringtones_bundle']).distinct()
>>> testme = models.BundlePack.objects.extra(where=[filter_string, bundle_join_string, category_join_string]).extra(tables=['ringtones_bundle_category', 'ringtones_bundle']).annotate(num_cat=Count('id'))
>>> testme[0].num_cat
>>> test = models.Ringtone.objects.extra(where=[string, category_join_string]).extra(tables=['ringtones_ringtone_category']).annotate(cat_count=Count("id"))
>>> reset_queries()
>>> connection.queries
>>> foo = list(map(int, categories.split(',')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment