summaryrefslogtreecommitdiff
path: root/django/contrib/contenttypes
diff options
context:
space:
mode:
authorMads Jensen <mje@inducks.org>2017-03-09 16:17:41 +0100
committerTim Graham <timograham@gmail.com>2017-06-28 14:07:55 -0400
commit550cb3a365dee4edfdd1563224d5304de2a57fda (patch)
treefb532f38774ff7619edd2a4532c3daae1ee0ac5a /django/contrib/contenttypes
parent43a4835edf32c57eb74c0eb207c276734a34abcf (diff)
Fixed #27818 -- Replaced try/except/pass with contextlib.suppress().
Diffstat (limited to 'django/contrib/contenttypes')
-rw-r--r--django/contrib/contenttypes/fields.py5
-rw-r--r--django/contrib/contenttypes/models.py5
-rw-r--r--django/contrib/contenttypes/views.py10
3 files changed, 8 insertions, 12 deletions
diff --git a/django/contrib/contenttypes/fields.py b/django/contrib/contenttypes/fields.py
index 9394182aee..57022ad1ef 100644
--- a/django/contrib/contenttypes/fields.py
+++ b/django/contrib/contenttypes/fields.py
@@ -1,4 +1,5 @@
from collections import defaultdict
+from contextlib import suppress
from django.contrib.contenttypes.models import ContentType
from django.core import checks
@@ -237,10 +238,8 @@ class GenericForeignKey:
if ct_id is not None:
ct = self.get_content_type(id=ct_id, using=instance._state.db)
- try:
+ with suppress(ObjectDoesNotExist):
rel_obj = ct.get_object_for_this_type(pk=pk_val)
- except ObjectDoesNotExist:
- pass
setattr(instance, self.cache_attr, rel_obj)
return rel_obj
diff --git a/django/contrib/contenttypes/models.py b/django/contrib/contenttypes/models.py
index ad2e0bc904..066474e49f 100644
--- a/django/contrib/contenttypes/models.py
+++ b/django/contrib/contenttypes/models.py
@@ -1,4 +1,5 @@
from collections import defaultdict
+from contextlib import suppress
from django.apps import apps
from django.db import models
@@ -38,10 +39,8 @@ class ContentTypeManager(models.Manager):
for the same model don't hit the database.
"""
opts = self._get_opts(model, for_concrete_model)
- try:
+ with suppress(KeyError):
return self._get_from_cache(opts)
- except KeyError:
- pass
# The ContentType entry was not found in the cache, therefore we
# proceed to load or create it.
diff --git a/django/contrib/contenttypes/views.py b/django/contrib/contenttypes/views.py
index d67f071569..c527687250 100644
--- a/django/contrib/contenttypes/views.py
+++ b/django/contrib/contenttypes/views.py
@@ -1,3 +1,5 @@
+from contextlib import suppress
+
from django.apps import apps
from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.requests import RequestSite
@@ -53,12 +55,10 @@ def shortcut(request, content_type_id, object_id):
# First, look for an many-to-many relationship to Site.
for field in opts.many_to_many:
if field.remote_field.model is Site:
- try:
+ with suppress(IndexError):
# Caveat: In the case of multiple related Sites, this just
# selects the *first* one, which is arbitrary.
object_domain = getattr(obj, field.name).all()[0].domain
- except IndexError:
- pass
if object_domain is not None:
break
@@ -77,10 +77,8 @@ def shortcut(request, content_type_id, object_id):
# Fall back to the current site (if possible).
if object_domain is None:
- try:
+ with suppress(Site.DoesNotExist):
object_domain = Site.objects.get_current(request).domain
- except Site.DoesNotExist:
- pass
else:
# Fall back to the current request's site.