Preventing access to models during migrations (RunPython, specifically)

It comes up from time to time in django IRC that someone is getting an error running tests and it turns out they’ve imported models in a RunPython operation.

What is the general feel about looking to prevent access to models (as if they were not ready yet, perhaps) when performing migrations?

Is there anything that could go wrong with this idea? Maybe something do to with the storage of the migrations in the database?

This is definitely something I considered while writing them, and I think it’s still a good idea, as it’s something that won’t go wrong when you write it but will later down the road (it also happens when you refactor code).

My main concern at this point would be backwards-incompatability - we’d have to have a protect_models=True flag in all new generated migrations and then look at that for enforcement - but I’d still be in favour of doing it.

Yeah, I figured we’d have to do that - which is rather annoying because it would not catch existing migrations that have the problem. The last one I saw was (I believe) an existing migration that worked until a later model change added a field - at which point in time the migration began to fail.

Perhaps we could have a warning whenever direct imports or global_apps.get_model() is used.

(Having said that, I’m not sure if it’s even going to be feasible at this stage: initial attempt was not entirely successful).