What are options for keeping two Django apps separate but still
being able to read and write to/from one Django app to
the database of the other Django app?
I’m aware of the nice Django docs on multiple databases (Multiple databases | Django documentation | Django )
I’m just not sure if that requires blending the models.py files together
of both Django apps or not.
Is the link above the only sane way to have two Django apps talk to each
other? Can I do that and keep the models.py somehow still separate
and not have the models blend together and “leak” into the “wrong” databases?
Your Models do not determine what database is used for any particular operation. That is controlled solely by your DATABASES settings.
The Models are a mapping layer between database tables and the rest of your Python code. They are neither “assigned to” nor “associated with” any particular database.
Those are two separate and independent steps. It’s only the migrate command that affects a database.
Side note: The makemigrations needs access to a database in a limited way. (In theory, it would be possible to do a version of the makemigrations command without any access to a database.)
But, I’m getting the impression that you’re still looking at this from the wrong angle.
A model doesn’t “go with” any database. A model is a Python representation of a database table. The database being used to populate that model is defined by the DATABASES setting, combined with the using function and (possibly) a database router.
I would assume you’d put some models as non-managed, if those are in another DB. But really this sounds like an XY problem and you shouldn’t be doing this at all. If a graph of functions accesses the same DB as another graph of functions, those graphs are both the same project and should be one django project imo.
If two Django projects are completely separate, is it possible to somehow bring them together into the same project? What do you mean by the XY problem?
Note - my opinion is that whether or not you want to merge two projects depends upon what data is shared.
If the two projects are going to share the same users, groups, and “Django infrastructure” information, then you might be better off merging them into one project. (The “bringing them together” could be as simple as creating a second app for the second project.)
On the other hand, if they are just sharing some “business data”, but the purposes for which that data is being used is different, then it’s quite ok to keep them as separate projects. However, only one of the projects should be responsible for doing the migrations - all other projects should identify those models as unmanaged. (This is something we do frequently.)