Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merging in master

Signed-off-by: Jason Myers <jason@jasonamyers.com>
  • Loading branch information...
commit 4f151da1e5e9bf527548a5737d9cd314e3abc68e 2 parents 6f07027 + f40f90d
Jason Myers jasonamyers authored
6 django/contrib/gis/db/models/sql/aggregates.py
View
@@ -1,7 +1,11 @@
-from django.db.models.sql.aggregates import Aggregate
+from django.db.models.sql import aggregates
+from django.db.models.sql.aggregates import * # NOQA
from django.contrib.gis.db.models.fields import GeometryField
+__all__ = ['Collect', 'Extent', 'Extent3D', 'MakeLine', 'Union'] + aggregates.__all__
+
+
class GeoAggregate(Aggregate):
# Default SQL template for spatial aggregates.
sql_template = '%(function)s(%(field)s)'
4 django/db/models/sql/aggregates.py
View
@@ -5,6 +5,10 @@
from django.db.models.fields import IntegerField, FloatField
+
+__all__ = ['Aggregate', 'Avg', 'Count', 'Max', 'Min', 'StdDev', 'Sum', 'Variance']
+
+
# Fake fields used to identify aggregate types in data-conversion operations.
ordinal_aggregate_field = IntegerField()
computed_aggregate_field = FloatField()
6 django/forms/models.py
View
@@ -134,7 +134,11 @@ def model_to_dict(instance, fields=None, exclude=None):
data[f.name] = []
else:
# MultipleChoiceWidget needs a list of pks, not object instances.
- data[f.name] = list(f.value_from_object(instance).values_list('pk', flat=True))
+ qs = f.value_from_object(instance)
+ if qs._result_cache is not None:
+ data[f.name] = [item.pk for item in qs]
+ else:
+ data[f.name] = list(qs.values_list('pk', flat=True))
else:
data[f.name] = f.value_from_object(instance)
return data
19 tests/admin_widgets/tests.py
View
@@ -2,7 +2,12 @@
from __future__ import unicode_literals
from datetime import datetime, timedelta
-from unittest import TestCase
+from unittest import TestCase, skipIf
+
+try:
+ import pytz
+except ImportError:
+ pytz = None
from django import forms
from django.conf import settings
@@ -641,6 +646,7 @@ class DateTimePickerSeleniumIETests(DateTimePickerSeleniumFirefoxTests):
webdriver_class = 'selenium.webdriver.ie.webdriver.WebDriver'
+@skipIf(pytz is None, "this test requires pytz")
@override_settings(TIME_ZONE='Asia/Singapore')
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
class DateTimePickerShortcutsSeleniumFirefoxTests(AdminSeleniumWebDriverTestCase):
@@ -660,9 +666,18 @@ def test_date_time_picker_shortcuts(self):
"""
self.admin_login(username='super', password='secret', login_url='/')
- now = datetime.now()
error_margin = timedelta(seconds=10)
+ # If we are neighbouring a DST, we add an hour of error margin.
+ tz = pytz.timezone('America/Chicago')
+ utc_now = datetime.now(pytz.utc)
+ tz_yesterday = (utc_now - timedelta(days=1)).astimezone(tz).tzname()
+ tz_tomorrow = (utc_now + timedelta(days=1)).astimezone(tz).tzname()
+ if tz_yesterday != tz_tomorrow:
+ error_margin += timedelta(hours=1)
+
+ now = datetime.now()
+
self.selenium.get('%s%s' % (self.live_server_url,
'/admin_widgets/member/add/'))
35 tests/model_forms/tests.py
View
@@ -828,6 +828,41 @@ def test_model_to_dict_many_to_many(self):
# Ensure many-to-many relation appears as a list
self.assertIsInstance(d['categories'], list)
+ def test_reuse_prefetched(self):
+ # model_to_dict should not hit the database if it can reuse
+ # the data populated by prefetch_related.
+ categories = [
+ Category(name='TestName1', slug='TestName1', url='url1'),
+ Category(name='TestName2', slug='TestName2', url='url2'),
+ Category(name='TestName3', slug='TestName3', url='url3')
+ ]
+ for c in categories:
+ c.save()
+ writer = Writer(name='Test writer')
+ writer.save()
+
+ art = Article(
+ headline='Test article',
+ slug='test-article',
+ pub_date=datetime.date(1988, 1, 4),
+ writer=writer,
+ article='Hello.'
+ )
+ art.save()
+ for c in categories:
+ art.categories.add(c)
+
+ art = Article.objects.prefetch_related('categories').get(pk=art.pk)
+
+ with self.assertNumQueries(0):
+ d = model_to_dict(art)
+
+ #Ensure all many-to-many categories appear in model_to_dict
+ for c in categories:
+ self.assertIn(c.pk, d['categories'])
+ #Ensure many-to-many relation appears as a list
+ self.assertIsInstance(d['categories'], list)
+
class OldFormForXTests(TestCase):
def test_base_form(self):
Please sign in to comment.
Something went wrong with that request. Please try again.