diff options
Diffstat (limited to 'django/contrib/contenttypes')
| -rw-r--r-- | django/contrib/contenttypes/fields.py | 5 | ||||
| -rw-r--r-- | django/contrib/contenttypes/models.py | 5 | ||||
| -rw-r--r-- | django/contrib/contenttypes/views.py | 10 |
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. |
