#36846: ContentType.get_object_for_this_type() does not handle removed models
-------------------------------------+-------------------------------------
     Reporter:  Maarten ter Huurne   |                    Owner:  Vishy
         Type:                       |  Algo
  Cleanup/optimization               |                   Status:  new
    Component:                       |                  Version:  dev
  contrib.contenttypes               |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

 * resolution:  wontfix =>
 * stage:  Unreviewed => Accepted
 * status:  closed => new

Comment:

 In other words, I'd be happy to consider a PR that changed
 `AttributeError` to `LookupError`, but I don't think I'd catch it in the
 admin without a wider discussion.

 {{{#!diff
 diff --git a/django/contrib/contenttypes/models.py
 b/django/contrib/contenttypes/models.py
 index 1ae45dea95..cba86ac689 100644
 --- a/django/contrib/contenttypes/models.py
 +++ b/django/contrib/contenttypes/models.py
 @@ -1,6 +1,7 @@
  from collections import defaultdict

  from django.apps import apps
 +from django.core.exceptions import ObjectDoesNotExist
  from django.db import models
  from django.db.models import Q
  from django.utils.translation import gettext_lazy as _
 @@ -176,7 +177,10 @@ class ContentType(models.Model):
          method. The ObjectNotExist exception, if thrown, will not be
 caught,
          so code that calls this method should catch it.
          """
 -        return
 self.model_class()._base_manager.using(using).get(**kwargs)
 +        if (model_class := self.model_class()) is None:
 +            # Raise the LookupError.
 +            return apps.get_model(self.app_label, self.model)
 +        return model_class._base_manager.using(using).get(**kwargs)

      def get_all_objects_for_this_type(self, **kwargs):
          """
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36846#comment:5>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/0107019b9e10683b-f91d26da-d880-43bc-beb4-aca2c44637ea-000000%40eu-central-1.amazonses.com.

Reply via email to