Has there ever been an effort to decouple the ORM so it can work seamlessly with Flask/FastAPI? If not, why?
This works:
import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
django.setup()
# And you're good...
You can do settings.configure() if you donāt want to provide the settings module.
I hope that helps.
Isnāt that importing the entire django library?
Are there any known caveats when using the ORM this way with Flask?
import django just imports the root django module. Thereās very little in there.
TBH, I canāt see why youād not just use Django if youāre using the ORM. Normally people say, āI donāt want to use the ORM so I wonāt use Djangoā. But, no real issues, beyond missing out on all the good bits that Django gives you that go with the ORM without you having to do anything else. (The admin, forms, serialisers, and so on, not to mention third-party librariesā¦)
However, your question was, can you do it, to which yes you can. Have fun! 
Cool thread here!
A use case of using just the ORM of Django doesnāt necessarily mean a web app, I have some services which I would love to be able to use the ORM (with migrations) of Django in.
Iāve got a couple of apps that donāt serve web pages at all. They exists only as a model layer and management commands. Itās useful having one framework servicing a number of different needs.
I found this repo. I have tried to use it. However it did not work properly. Maybe commits from experienced Django developers make this project more useful.
Hi @msenol86 ā I donāt think an extra library is needed here. See the example I gave above:
Itās four lines to be able to you the ORM in a stand alone script. Itās not worth the maintenance cost of extra code on top of that Iād think. ![]()
But, those four lines are super unintuitive. I think if there was a āconnectā function that would just do this in the background, then djangoās ORM could probably replace every other ORM library in the python ecosystem.
Thatās probably not the devsā goal or interest, but they did make a python ORM that pretty much blows away everything else in terms of ease of use (besides maybe the dataset library).
As a proof of concept: Truly Standalone Django-ORM Wrapper Ā· GitHub