# Django ORM as a standalone library

**URL:** <https://forum.djangoproject.com/t/django-orm-as-a-standalone-library/4971>\
**Category:** Using the ORM\
**Created:** [October 27, 2020, 1:27pm UTC](https://forum.djangoproject.com/t/django-orm-as-a-standalone-library/4971 "2020-10-27T13:27:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![avremel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/avremel/32/2085_2.png) [@avremel](https://forum.djangoproject.com/u/avremel)\
**Post date:** [October 27, 2020, 1:27pm UTC](https://forum.djangoproject.com/t/django-orm-as-a-standalone-library/4971/1 "2020-10-27T13:27:39Z")

</div>

Has there ever been an effort to decouple the ORM so it can work seamlessly with Flask/FastAPI? If not, why?

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [October 27, 2020, 1:52pm UTC](https://forum.djangoproject.com/t/django-orm-as-a-standalone-library/4971/2 "2020-10-27T13:52:41Z")

</div>

This works:

```auto
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.

---

<div class="post-metadata">

**Author:** ![avremel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/avremel/32/2085_2.png) [@avremel](https://forum.djangoproject.com/u/avremel)\
**Post date:** [October 27, 2020, 2:09pm UTC](https://forum.djangoproject.com/t/django-orm-as-a-standalone-library/4971/3 "2020-10-27T14:09:02Z")

</div>

Isn’t that importing the entire django library?  
Are there any known caveats when using the ORM this way with Flask?

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [October 27, 2020, 2:16pm UTC](https://forum.djangoproject.com/t/django-orm-as-a-standalone-library/4971/4 "2020-10-27T14:16:55Z")

</div>

`import django` just imports the root django module. [There’s very little in there](https://github.com/django/django/blob/master/django/ __init__.py).

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! 😄

---

<div class="post-metadata">

**Author:** ![yankyhoffman](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/yankyhoffman/32/2317_2.png) [@yankyhoffman](https://forum.djangoproject.com/u/yankyhoffman)\
**Post date:** [October 28, 2020, 12:48am UTC](https://forum.djangoproject.com/t/django-orm-as-a-standalone-library/4971/5 "2020-10-28T00:48:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 28, 2020, 1:52am UTC](https://forum.djangoproject.com/t/django-orm-as-a-standalone-library/4971/6 "2020-10-28T01:52:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![msenol86](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/msenol86/32/5546_2.png) [@msenol86](https://forum.djangoproject.com/u/msenol86)\
**Post date:** [August 20, 2021, 1:43pm UTC](https://forum.djangoproject.com/t/django-orm-as-a-standalone-library/4971/7 "2021-08-20T13:43:40Z")

</div>

I found this [repo](https://github.com/jasonalantolbert/standalorm). I have tried to use it. However it did not work properly. Maybe commits from experienced Django developers make this project more useful.

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [August 21, 2021, 3:37pm UTC](https://forum.djangoproject.com/t/django-orm-as-a-standalone-library/4971/8 "2021-08-21T15:37:25Z")

</div>

Hi @msenol86 — I don’t think an extra library is needed here. See the example I gave above:

> [@Django ORM as a standalone library](https://forum.djangoproject.com/t/django-orm-as-a-standalone-library/4971/2):
>
> 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.

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. 🤔

---

<div class="post-metadata">

**Author:** ![mw3i](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mw3i/32/16118_2.png) [@mw3i](https://forum.djangoproject.com/u/mw3i)\
**Post date:** [September 25, 2023, 1:00pm UTC](https://forum.djangoproject.com/t/django-orm-as-a-standalone-library/4971/9 "2023-09-25T13:00:49Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![mw3i](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mw3i/32/16118_2.png) [@mw3i](https://forum.djangoproject.com/u/mw3i)\
**Post date:** [September 26, 2023, 6:12pm UTC](https://forum.djangoproject.com/t/django-orm-as-a-standalone-library/4971/10 "2023-09-26T18:12:13Z")

</div>

As a proof of concept: [Truly Standalone Django-ORM Wrapper · GitHub](https://gist.github.com/mw3i/b879895272a28d1c789f23ee91555620)
