Skip to content

Instantly share code, notes, and snippets.

from django.db import connection
class QueryCountMiddleware(object):
def process_response(self, request, response):
duration = 0
for query in connection.queries:
duration += float(query['time'])
print ('%s\n' % query['sql'])
diff --git a/django/contrib/auth/models.py b/django/contrib/auth/models.py
index 5b89fe8..e8c796a 100644
--- a/django/contrib/auth/models.py
+++ b/django/contrib/auth/models.py
@@ -13,7 +13,6 @@ from django.contrib import auth
from django.contrib.auth.hashers import (
check_password, make_password, is_password_usable)
from django.contrib.auth.signals import user_logged_in
-from django.contrib.contenttypes.models import ContentType
from django.utils.encoding import python_2_unicode_compatible
def __getitem__(self, key):
+ if hasattr(self, key):
+ return getattr(self, key)
...
loic84: FunkyBob: so you could customize the QS? like limit the number of related objects pulled from the db, or encapsulate more logic in QS methods.
13:49 loic84: say you want Blog.objects.get_queryset() to automatically perform a bunch of select_related/prefetch_related
13:50 loic84: when you do Article.objects.select_related('blog'), these are ignored.
13:51 loic84: so then you are left with 2 options, either you use prefetch_related('block') rather than select_related so you can use your custom manager, or you duplicate all the logic in the original select_related().
13:52 loic84: also you may want to select_related('blog') but defer() a bunch of fields
@loic
loic / -
Created February 19, 2014 19:23
test
@loic
loic / -
Created February 19, 2014 19:27
test
diff --git a/django/db/migrations/state.py b/django/db/migrations/state.py
index 79a4411..ad9fb07 100644
--- a/django/db/migrations/state.py
+++ b/django/db/migrations/state.py
@@ -1,3 +1,5 @@
+from collections import OrderedDict
+
from django.apps import AppConfig
from django.apps.registry import Apps
from django.db import models
diff --git a/django/contrib/contenttypes/fields.py b/django/contrib/contenttypes/fields.py
index 39fbe0e..dab3d69 100644
--- a/django/contrib/contenttypes/fields.py
+++ b/django/contrib/contenttypes/fields.py
@@ -30,13 +30,11 @@ class GenericForeignKey(six.with_metaclass(RenameGenericForeignKeyMethods)):
fields.
"""
- def __init__(self, ct_field="content_type", fk_field="object_id", for_concrete_model=True,
- related_name=None):
class Foo(models.Model):
pass
class Bar(models.Model):
foo = models.ForeignKey(Foo, blank=True)
diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py
index 8bdce5d..2c8ab99 100644
--- a/tests/migrations/test_writer.py
+++ b/tests/migrations/test_writer.py
@@ -59,7 +59,11 @@ class WriterTests(TestCase):
self.assertSerializedEqual(1)
self.assertSerializedEqual(None)
self.assertSerializedEqual(b"foobar")
+ string, imports = MigrationWriter.serialize(b"foobar")
+ self.assertEqual(string, "b'foobar'")