-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Simon Willison
committed
Jan 10, 2010
0 parents
commit 632bd35
Showing
12 changed files
with
252 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
ve | ||
demo/data.db | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
django_lazymap | ||
============== | ||
|
||
Allows you to register a transforming map function with a Django QuerySet | ||
that will be executed only when the QuerySet itself has been evaluated. | ||
|
||
This allows you to build optimisations like "fetch all tags for these 10 rows" | ||
while still benefiting from Django's lazy QuerySet evaluation. | ||
|
||
For example: | ||
|
||
def lookup_tags(item_qs): | ||
item_pks = [item.pk for item in item_qs] | ||
m2mfield = Item._meta.get_field_by_name('tags')[0] | ||
tags_for_item = Tag.objects.filter( | ||
item__in = item_pks | ||
).extra(select = { | ||
'item_id': '%s.%s' % ( | ||
m2mfield.m2m_db_table(), m2mfield.m2m_column_name() | ||
) | ||
}) | ||
tag_dict = {} | ||
for tag in tags_for_item: | ||
tag_dict.setdefault(tag.item_id, []).append(tag) | ||
for item in item_qs: | ||
item.fetched_tags = tag_dict.get(item.pk, []) | ||
|
||
qs = Item.objects.filter(name__contains = 'e').lazymap(lookup_tags) | ||
|
||
for item in qs: | ||
print item, item.fetched_tags | ||
|
||
Prints: | ||
|
||
Winter comes to Ogglesbrook [<sledging>, <snow>, <winter>, <skating>] | ||
Summer now [<skating>, <sunny>] | ||
|
||
But only executes two SQL queries - one to fetch the items, and one to fetch ALL of the tags for those items. | ||
|
||
Since the lazymap function can transform an evaluated QuerySet, it | ||
doesn't need to make extra database calls at all - it should work well for | ||
things like cache.multi_get() too. | ||
|
||
Inspired by http://github.com/lilspikey/django-batch-select/ |
Empty file.
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from models import Tag, Item | ||
from django.contrib import admin | ||
|
||
admin.site.register(Tag) | ||
admin.site.register(Item) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from django.db import models | ||
from django_lazymap import LazyMapManager | ||
|
||
class Tag(models.Model): | ||
name = models.CharField(max_length = 255) | ||
|
||
def __unicode__(self): | ||
return self.name | ||
|
||
class Item(models.Model): | ||
name = models.CharField(max_length = 255) | ||
tags = models.ManyToManyField(Tag) | ||
|
||
objects = LazyMapManager() | ||
|
||
def __unicode__(self): | ||
return self.name |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
""" | ||
This file demonstrates two different styles of tests (one doctest and one | ||
unittest). These will both pass when you run "manage.py test". | ||
Replace these with more appropriate tests for your application. | ||
""" | ||
|
||
from django.test import TestCase | ||
|
||
class SimpleTest(TestCase): | ||
def test_basic_addition(self): | ||
""" | ||
Tests that 1 + 1 always equals 2. | ||
""" | ||
self.failUnlessEqual(1 + 1, 2) | ||
|
||
__test__ = {"doctest": """ | ||
Another way to test that 1 + 1 is equal to 2. | ||
>>> 1 + 1 == 2 | ||
True | ||
"""} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Create your views here. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env python | ||
from django.core.management import execute_manager | ||
try: | ||
import settings # Assumed to be in the same directory. | ||
except ImportError: | ||
import sys | ||
sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__) | ||
sys.exit(1) | ||
|
||
if __name__ == "__main__": | ||
execute_manager(settings) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# Django settings for demo project. | ||
|
||
DEBUG = True | ||
TEMPLATE_DEBUG = DEBUG | ||
|
||
ADMINS = ( | ||
# ('Your Name', '[email protected]'), | ||
) | ||
|
||
MANAGERS = ADMINS | ||
|
||
DATABASE_ENGINE = 'sqlite3' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. | ||
DATABASE_NAME = 'data.db' # Or path to database file if using sqlite3. | ||
DATABASE_USER = '' # Not used with sqlite3. | ||
DATABASE_PASSWORD = '' # Not used with sqlite3. | ||
DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3. | ||
DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3. | ||
|
||
# Local time zone for this installation. Choices can be found here: | ||
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name | ||
# although not all choices may be available on all operating systems. | ||
# If running in a Windows environment this must be set to the same as your | ||
# system time zone. | ||
TIME_ZONE = 'America/Chicago' | ||
|
||
# Language code for this installation. All choices can be found here: | ||
# http://www.i18nguy.com/unicode/language-identifiers.html | ||
LANGUAGE_CODE = 'en-us' | ||
|
||
SITE_ID = 1 | ||
|
||
# If you set this to False, Django will make some optimizations so as not | ||
# to load the internationalization machinery. | ||
USE_I18N = True | ||
|
||
# Absolute path to the directory that holds media. | ||
# Example: "/home/media/media.lawrence.com/" | ||
MEDIA_ROOT = '' | ||
|
||
# URL that handles the media served from MEDIA_ROOT. Make sure to use a | ||
# trailing slash if there is a path component (optional in other cases). | ||
# Examples: "http://media.lawrence.com", "http://example.com/media/" | ||
MEDIA_URL = '' | ||
|
||
# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a | ||
# trailing slash. | ||
# Examples: "http://foo.com/media/", "/media/". | ||
ADMIN_MEDIA_PREFIX = '/media/' | ||
|
||
# Make this unique, and don't share it with anybody. | ||
SECRET_KEY = 'hea-c%x=u^&2bypgp81_+tfmxkt3l-ni-3$(yml%d=!@9&+u2x' | ||
|
||
# List of callables that know how to import templates from various sources. | ||
TEMPLATE_LOADERS = ( | ||
'django.template.loaders.filesystem.load_template_source', | ||
'django.template.loaders.app_directories.load_template_source', | ||
# 'django.template.loaders.eggs.load_template_source', | ||
) | ||
|
||
MIDDLEWARE_CLASSES = ( | ||
'django.middleware.common.CommonMiddleware', | ||
'django.contrib.sessions.middleware.SessionMiddleware', | ||
'django.contrib.auth.middleware.AuthenticationMiddleware', | ||
) | ||
|
||
ROOT_URLCONF = 'demo.urls' | ||
|
||
TEMPLATE_DIRS = ( | ||
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". | ||
# Always use forward slashes, even on Windows. | ||
# Don't forget to use absolute paths, not relative paths. | ||
) | ||
|
||
INSTALLED_APPS = ( | ||
'django.contrib.auth', | ||
'django.contrib.contenttypes', | ||
'django.contrib.sessions', | ||
'django.contrib.sites', | ||
'django.contrib.admin', | ||
'demo_models', | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from django.conf.urls.defaults import * | ||
from django.contrib import admin | ||
from django.http import HttpResponse | ||
from django.db import connection | ||
|
||
from demo_models.models import Item, Tag | ||
|
||
from pprint import pformat | ||
|
||
admin.autodiscover() | ||
|
||
def example(request): | ||
def lookup_tags(item_qs): | ||
item_pks = [item.pk for item in item_qs] | ||
m2mfield = Item._meta.get_field_by_name('tags')[0] | ||
tags_for_item = Tag.objects.filter( | ||
item__in = item_pks | ||
).extra(select = { | ||
'item_id': '%s.%s' % ( | ||
m2mfield.m2m_db_table(), m2mfield.m2m_column_name() | ||
) | ||
}) | ||
tag_dict = {} | ||
for tag in tags_for_item: | ||
tag_dict.setdefault(tag.item_id, []).append(tag) | ||
for item in item_qs: | ||
item.fetched_tags = tag_dict.get(item.pk, []) | ||
|
||
qs = Item.objects.all().lazymap(lookup_tags) | ||
|
||
s = [] | ||
|
||
for item in qs: | ||
s.append('%s: %s' % (item, [t.name for t in item.fetched_tags])) | ||
|
||
return HttpResponse( | ||
'<br>'.join(s) + '<pre>%s</body></html>' % pformat(connection.queries) | ||
) | ||
|
||
urlpatterns = patterns('', | ||
(r'^$', example), | ||
(r'^admin/', include(admin.site.urls)), | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from django.db import models | ||
|
||
class LazyMapQuerySet(models.query.QuerySet): | ||
def __init__(self, *args, **kwargs): | ||
super(LazyMapQuerySet, self).__init__(*args, **kwargs) | ||
self._lazymap_fns = [] | ||
|
||
def lazymap(self, fn): | ||
self._lazymap_fns.append(fn) | ||
return self | ||
|
||
def iterator(self): | ||
result_iter = super(LazyMapQuerySet, self).iterator() | ||
if self._lazymap_fns: | ||
results = list(result_iter) | ||
for fn in self._lazymap_fns: | ||
fn(results) | ||
return iter(results) | ||
return result_iter | ||
|
||
class LazyMapManager(models.Manager): | ||
|
||
def get_query_set(self): | ||
return LazyMapQuerySet(self.model) |