Reverse look fails when importing from the admin for an app

15 views
Skip to first unread message

Derek

unread,
Oct 3, 2021, 5:14:34 AM10/3/21
to django-users
My pytest suite is currently not working, as the reverse lookup is failing for admin views; for example, trying to access `reverse('admin:myapp_mymodel_changelist')` or `reverse_lazy('admin:myapp_mymodel_changelist')`.

I have narrowed down the cause to the trigger being *any* kind of import being made from the `admin.py` code for that app; the available URLs are still there for all the other apps but *not* for the one being imported.

In `django/urls/resolvers.py` package, the available possible matches are determined here:

    possibilities = self.reverse_dict.getlist(lookup_view)

And one can add a debug statement which shows all the keys:

    keys = [key for key in [*self.reverse_dict] if isinstance(key, str)]
    print(keys)

Some basic test code, which I run in the Django shell, demonstrates how the failure is triggered, and shows that this is not an artefact related to the tests themselves:

    from django.core.urlresolvers import reverse_lazy
    from myapp.admin import MyModelAdmin
    print(reverse_lazy('admin:myapp_mymodel_changelist'))

But this passes:

    from django.core.urlresolvers import reverse_lazy
    print(reverse_lazy('admin:myapp_mymodel_changelist'))

i.e. if I comment out the import line the code works, otherwise it fails.

Any ideas on why Django is failing when importing from the app's admin?

Thanks
Derek
Reply all
Reply to author
Forward
0 new messages