Ticket #25692: add_natural_keys_to_site.patch

File add_natural_keys_to_site.patch, 1.7 KB (added by j0hnsmith, 7 years ago)
  • django/contrib/sites/models.py

    diff --git a/django/contrib/sites/models.py b/django/contrib/sites/models.py
    index e22fd40..7ee7e09 100644
    a b class SiteManager(models.Manager): 
    7878        global SITE_CACHE
    7979        SITE_CACHE = {}
    8080
     81    def get_by_natural_key(self, domain):
     82        return self.get(domain=domain)
     83
    8184
    8285@python_2_unicode_compatible
    8386class Site(models.Model):
    class Site(models.Model): 
    9699    def __str__(self):
    97100        return self.domain
    98101
     102    def natural_key(self):
     103        return (self.domain,)
     104
    99105
    100106def clear_site_cache(sender, **kwargs):
    101107    """
  • docs/releases/1.10.txt

    diff --git a/docs/releases/1.10.txt b/docs/releases/1.10.txt
    index 4f6ff55..18fc4a8 100644
    a b Minor features 
    9393:mod:`django.contrib.sites`
    9494^^^^^^^^^^^^^^^^^^^^^^^^^^^
    9595
    96 * ...
     96* The ``django.contrib.sites.models.Site`` model now supports
     97  :ref:`natural keys <topics-serialization-natural-keys>`.
    9798
    9899:mod:`django.contrib.staticfiles`
    99100^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  • tests/sites_tests/tests.py

    diff --git a/tests/sites_tests/tests.py b/tests/sites_tests/tests.py
    index a5ce7a6..6bf7ce4 100644
    a b class SitesFrameworkTests(TestCase): 
    179179        with self.assertRaisesMessage(ValidationError, msg):
    180180            site.validate_unique()
    181181
     182    def test_site_natural_key(self):
     183        self.assertEqual(Site.objects.get_by_natural_key(self.site.domain), self.site)
     184        self.assertEqual(self.site.natural_key(), (self.site.domain,))
     185
    182186
    183187class JustOtherRouter(object):
    184188    def allow_migrate(self, db, app_label, **hints):
Back to Top