Fixed bug in circular dependency algo for migration dependencies.

Previous algo only worked if the first item was a part of the loop,
and you would get an infinite loop otherwise (see test).

To fix this, it was much easier to do a pre-pass.

A bonus is that you now get an error message that actually helps you debug
the dependency problem.
Fixed flake8 warnings.
Fixed #23898 -- Added missing context to admin's deleted_selected view.

Thanks Redouane Zait for the report.