Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing multiple Django projects from a single monorepo

Tags:

python

django

I want to be able to write shared functions that can be accessed in one-off batch scripts and also by the running Django service (to use the ORM)

Currently, I have this in the _init__.py under the my_proj module.

if 'DJANGO_SETTINGS_MODULE' not in os.environ:
    os.environ['DJANGO_SETTINGS_MODULE'] = 'my_proj.blah.blah.settings'
    import django
    django.setup()

This works fine for one django project. However, now I want to do reference the ORM functions from another django project, "other_proj" in the same repo from an independent script that lives outside both django projects.

Is there a way to "django.setup()" multiple projects at once?

Or, at least, a way to easily toggle the setup between the two projects?

Or is there a better way altogether? (I realize I could create a client library to hit the services while they are running, but would prefer to remove that overhead)

like image 466
kentkolze Avatar asked Oct 23 '25 07:10

kentkolze


1 Answers

If you want a Django project to access functionality that resides in a different Django project, a client library is an option (as you noted). You could also consider packaging those sets of functionality as re-usable Django apps that you import into each project, or you could abstract them further into re-usable Python modules which get imported into each project. If you're hoping to use the Django ORM from one Project to access data from a different project, then you might be looking for this SO question: How to make two django projects share the same database

I think with more specifics in your question (such as, for example, function X in Project A you wish you could call from Project B) we might be able to be more specific with guidance.

like image 193
jchung Avatar answered Oct 25 '25 21:10

jchung



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!