[Answered ]-Use model from the versioned app registry globally

2👍

The versioned app registries are not globally accessible. You could pass the model as a parameter to the function, and use the current model as the default:

from my_app.models import Bar

def my_function(..., bar_model=Bar):
    # Use bar_model instead of Bar

# Your RunPython function
def migrate_something(apps, schema_editor):
    my_function(bar_model=apps.get_model('my_app', 'bar'))

You don’t have to pass the bar_model parameter if you call it from regular code, but when calling it from a migration you can pass the historical model.

If you need multiple models you could pass apps instead:

from django.apps import apps as global_apps

def my_function(..., apps=global_apps):
    Bar = apps.get_model('my_app', 'bar')
👤knbk

Leave a comment