App initialization dependency (Signal ready)

I have an app that needs to insert records into the database of another app during the initialization phase so that the user does not have to do this manually.

I tried using the ready and post_migrate signals, but when MyApp is called, the other app, OtherApp, is not fully initialized yet. The ContentType objects that should be present after the complete initialization of OtherApp are still missing. I checked this with the following code:

for ct in ContentType.objects.filter(app_label=other_app_label): print(f"ContentType: app_label={ct.app_label}, model={ct.model}")

How can I ensure that OtherApp is fully initialized before MyApp is initialized? The ready signal obviously doesn’t work here.

Welcome @jifox !

Can you be more specific and provide details of exactly what you’re trying to accomplish here?

Superficially, what you’re asking for has what is frequently referred to as a “bad code smell”. This is especially true in a production environment where you’re usually running multiple instances of your process. You’re either going to insert multiple copies of this data, or else have to deal with possible errors associated with the insertion of duplicate rows. Even if you’re set up to handle these errors, it’s still going to try to do this every time a process instance is restarted.

In every situation that I’ve encountered, there’s a better way of handling situations like this that trying to do it during startup, but the specifics generally depend upon the particular situation.