From 0115f9faa517bba882a31b0ade076601b5a41de6 Mon Sep 17 00:00:00 2001 From: j0hnsmith Date: Fri, 6 Nov 2015 22:55:44 +0000 Subject: [PATCH] Fixed #25692 -- Added natural keys support to Site model. --- django/contrib/sites/models.py | 6 ++++++ docs/releases/1.10.txt | 3 ++- tests/sites_tests/tests.py | 4 ++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/django/contrib/sites/models.py b/django/contrib/sites/models.py index e22fd4021d..7ee7e091c0 100644 --- a/django/contrib/sites/models.py +++ b/django/contrib/sites/models.py @@ -78,6 +78,9 @@ class SiteManager(models.Manager): global SITE_CACHE SITE_CACHE = {} + def get_by_natural_key(self, domain): + return self.get(domain=domain) + @python_2_unicode_compatible class Site(models.Model): @@ -96,6 +99,9 @@ class Site(models.Model): def __str__(self): return self.domain + def natural_key(self): + return (self.domain,) + def clear_site_cache(sender, **kwargs): """ diff --git a/docs/releases/1.10.txt b/docs/releases/1.10.txt index 4f6ff558bc..1f3857ab28 100644 --- a/docs/releases/1.10.txt +++ b/docs/releases/1.10.txt @@ -93,7 +93,8 @@ Minor features :mod:`django.contrib.sites` ^^^^^^^^^^^^^^^^^^^^^^^^^^^ -* ... +* The :class:`~django.contrib.sites.models.Site` model now supports + :ref:`natural keys `. :mod:`django.contrib.staticfiles` ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/sites_tests/tests.py b/tests/sites_tests/tests.py index a5ce7a6779..6bf7ce4d0b 100644 --- a/tests/sites_tests/tests.py +++ b/tests/sites_tests/tests.py @@ -179,6 +179,10 @@ class SitesFrameworkTests(TestCase): with self.assertRaisesMessage(ValidationError, msg): site.validate_unique() + def test_site_natural_key(self): + self.assertEqual(Site.objects.get_by_natural_key(self.site.domain), self.site) + self.assertEqual(self.site.natural_key(), (self.site.domain,)) + class JustOtherRouter(object): def allow_migrate(self, db, app_label, **hints):