#36804: AdminSite.get_model_admin raises AttributeError when `to` model of a
ForeignKey is defined as a string
--------------------------------+-----------------------------------------
     Reporter:  Parth Paradkar  |                     Type:  Bug
       Status:  new             |                Component:  contrib.admin
      Version:  5.2             |                 Severity:  Normal
     Keywords:  admin           |             Triage Stage:  Unreviewed
    Has patch:  0               |      Needs documentation:  0
  Needs tests:  0               |  Patch needs improvement:  0
Easy pickings:  1               |                    UI/UX:  0
--------------------------------+-----------------------------------------
 I have a foreign key on one of my models defined as- \\


 {{{
 slack_alert = models.ForeignKey(
         "falcon.SlackAlert",
         on_delete=models.SET_NULL,
         null=True,
         blank=True,
         related_name="tickets",
     )
 }}}



 When I do not include the `falcon` app in `INSTALLED_APPS`, the admin site
 raises an error (which is expected). However, while handling the expected
 `KeyError`, it throws an `AttributeError`. I am guessing this is since it
 expects a `Model` instance.

 Traceback-


 {{{
 Traceback (most recent call last):
   File "/Users/parthparadkar/projects/lighthouse/venv/lib/python3.11/site-
 packages/django/contrib/admin/sites.py", line 170, in get_model_admin
     return self._registry[model]
            ~~~~~~~~~~~~~~^^^^^^^
 KeyError: 'falcon.SlackAlert'

 During handling of the above exception, another exception occurred:

   File "/Users/parthparadkar/projects/lighthouse/venv/lib/python3.11/site-
 packages/django/contrib/admin/checks.py", line 209, in
 _check_autocomplete_fields
     [
   File "/Users/parthparadkar/projects/lighthouse/venv/lib/python3.11/site-
 packages/django/contrib/admin/checks.py", line 210, in <listcomp>
     self._check_autocomplete_fields_item(
   File "/Users/parthparadkar/projects/lighthouse/venv/lib/python3.11/site-
 packages/django/contrib/admin/checks.py", line 239, in
 _check_autocomplete_fields_item
     related_admin =
 obj.admin_site.get_model_admin(field.remote_field.model)
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/Users/parthparadkar/projects/lighthouse/venv/lib/python3.11/site-
 packages/django/contrib/admin/sites.py", line 172, in get_model_admin
     raise NotRegistered(f"The model {model.__name__} is not registered.")
                                      ^^^^^^^^^^^^^^
 AttributeError: 'str' object has no attribute '__name__'. Did you mean:
 '__ne__'?

 }}}


 I think this can be fixed by checking with `isinstance` before raising
 `NotRegistered`?

 {{{
  except KeyError:
     if isinstance(model, str):
         raise NotRegistered(f"The model {model} is not registered.")
     else:
         raise NotRegistered(f"The model {model.__name__} is not
 registered.")
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36804>
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/0107019b271080ce-a98d03e8-2888-4178-80b6-32b2ccd2cf5e-000000%40eu-central-1.amazonses.com.

Reply via email to