11

I like the Django ORM. It's simple, easy to use, and reasonably powerful.

I'm currently developing some internal sites for the VFX company I work for, for which I've used Django. In the meantime, we are developing other python applications and libraries to be used in various contexts in production. There's a number of places in which our core library needs to be interacting with some databases, and using an ORM like Django would really help things. I'm aware of other options like SqlAlchemy or PeeWee, but I'd like to see if Django will work since I use it on the websites and I like its API better.

Using Django as an ORM in a library is tricky (as I explored in a previous question), because Django expects to be used as a website with "apps". In a library, I might want to define any number of data models, which would exist in appropriate places in the library but not inside any Django app (as we're not using any other parts of the framework). So far so good.

I can create a baseclass for my models anywhere in the library as follows:

from django.db import models
from django.apps import apps
import django.conf

django.conf_settings.configure(
    DATABASES = ...
)

apps.populate((__name__,))

class LibModel(models.Model):
    class Meta:
        abstract = True
        app_label = __name__

Then anywhere in the library I can create my own models with this baseclass. Since I'm not relying on the "app" for the database names, I need to state them explicitly.

class SpecificModel(LibModel):
    # fields go here
    class Meta(LibModel.Meta):
        db_table = "specific_model_table_name"

This gets around my concern of having to simulate the structure of an "app". The name property in the base class supplies Django with all it needs, and then Django quits whining about not finding an app. The other model files can live wherever they want.

However, there is a glaring use case where this all falls apart. Say that my Django web application wants to use some functionality from the company core python library, which now uses the Django ORM for various things. Since I make a call to django.conf.settings.configure in the library, Django is going to scream about defining the settings more than once when it tries to run the main application.

So basically, a library using the Django ORM is incompatible with Django. Wonderful.

Is there any way around this? I mean, it's a lovely ORM - is it really this impossible to use in a standalone modular way? Is the Django architecture utterly singleton in nature, making this impossible?

*Not a duplicate I'm trying to have a company python library that uses Django as an ORM. Some of the things that could depend on it might be Django websites themselves. How do I get around Django's singleton insistence on only setting the settings config once? Or is it possible? None of these answers address this!

Community
  • 1
  • 1
thegiffman
  • 465
  • 4
  • 8
  • Possible duplicate of [Use only some parts of Django?](http://stackoverflow.com/questions/302651/use-only-some-parts-of-django) – solarissmoke Jun 20 '16 at 04:30
  • Possible duplicate of [Using django for CLI tool](http://stackoverflow.com/questions/32088702/using-django-for-cli-tool) – e4c5 Jun 20 '16 at 05:04
  • This is *not* a duplicate. Questions mentioned as possible duplicates ask about much simpler problem. – Zbyl Jan 10 '20 at 14:03
  • Very interesting idea, Django ORM is a really one of the best with a much better syntax and styling comparing to alternatives like sqlalchemy & etc – Alexandr Shurigin Jan 11 '20 at 15:25
  • There is a simple difference between libraries and frameworks. Libraries don't do anything by themselves. They shouldn't even configure logging. They are passive actors that you import and use their pieces as you write. Frameworks on the other hand are the opposite: they *run*. It's your code that is meant to be called by the framework. Using a framework as a library is going to be a pain in the ass no matter what. – Giacomo Alzetta Jan 17 '20 at 11:00

2 Answers2

2

You can check if django has already been configured.

from django.apps import apps
from django.conf import settings

if not apps.ready:
    settings.configure()
    django.setup()

When starting Django application - core python library can be configured as separate app an be loaded on startup.

Also, check this answer on dynamic app loading at runtime.

Oleg Russkin
  • 4,234
  • 1
  • 8
  • 20
  • So if I understand correctly one could create a package with a Django app inside (using ORM). Then everyone who would like to use it would need to create a Django project (possibly standalone) and use this Django app. Alternatively the package could provide a way to bootstrap Django internally, so that it could be used completely standalone. It could also try to detect which mode it should operate in. But this might still have side effects and compatibility issues. – Zbyl Jan 13 '20 at 15:48
  • Yes. If using in another django app - this app should be created as `reusable app` and just specfied in another app `settings.py` `INSTALLED_APPS`. The reason for questioned manipulations - if importing in non-django python code - then this django needs to setup, load apps, etc, we need to provide settings for it. It may be some method / class in app that performs these actions (bootstraps) when imported in non-django python code. For just ORM it seems like it should be ok, something is even mentioned in django documentation. – Oleg Russkin Jan 13 '20 at 17:46
  • I wrote about a risk of this solution in my answer, but it will frequently work correctly and therefore +1. – hynekcer Jan 17 '20 at 11:02
2

A simple answer is how to initialize Django in a standalone application and do it compatible with Django applications.

import os
import django

if not 'DJANGO_SETTINGS_MODULE' in os.environ:
    os.environ['DJANGO_SETTINGS_MODULE'] = 'mysettings'

    # # or without DJANGO_SETTINGS_MODULE directly
    # from django.conf import settings
    # settings.configure(DATABASES=... other...)

    django.setup()

# this shouldn't be before DJANGO_SETTINGS_MODULE or settings.configure(...)
from myapp.models import MyModel
# this shouldn't be called before django.setup()
queryset = MyModel.objects.filter(...)

This is more compatible with Django then the answer by Oleg Russkin (where a risk of cyclic dependency at django.setup() is possible if the code is called inside inside a setup() initiated by another similar code or a normal Django project started by manage. It is similar to manage.py where django.setup() is also called internally by execute_from_command_line(sys.argv). The setup initializes all modules related to INSTALLED_APPS all urls modules and consequently all views etc. Many of them are lazy, but still. If any code called by setup() depends on this then neither the condition not apps.ready doesn't help. The setup() is not reentrant and the startup fails.)


Much more general answer

An important concept of Django is to support writing reusable parts of code ("applications" in Django terminology, that can be developed and tested independently. There can be also a tree of dependencies, but uncontrolled mutual dependencies should be avoided if possible) Reusable applications are expected that they can be easier combined to whole project ("project" in Django terminology is with all settings necessary to run it by Python.)

The only unavoidable and useful "singleton" in Django ORM are database connections django.db.connections and django.conf.settings especially INSTALLED_APPS. Only one connection should be used to the same database from the same process or thread.

Django is very configurable. An extreme example: It is possible to write a single file project where all code like settings, models, URL configs and views is defined in one file. That extreme that is probably useful only for some special tests or very short demos or as an exercise. It is even possible to define a project by one file with two "reusable" applications :-)

Django supports also "legacy databases" where the database structure is shared with existing non Django applications and models can be created by inspectdb command. Table names in such models are explicit and don't contain the app name. On the other hand the app name prefix is useful to prevent a possible conflict of the same table names from independent "applications". An important decision is if you can use it as a "legacy" database or a normal Django database.

You can decide between following two solutions or to combine them:

  • Use e.g. foo_models or bar.models and import all models from them e.g. to app.models and add only that "app" to INSTALLED_APPLICATIONS. This can be viable if it is only for one company and never otherwise and central name assigment is possible. (easiest but little naive)
  • Use some coarse separation of namespaces to several apps. You should probably use not more than one app with simple names without app name prefix.

Think ahead about migrations**. They will be probably very complicated and very soon impossible if you will create later more projects for the same database and different subsets of database tables without separating them to more apps and without app namespace.

There is really no "singleton" in Django ORM except of django.db.connections itself. If you use more databases you can direct some tables to a specific database by DATABASE_ROUTERS, even with two different models that use the same table name without a prefix.

hynekcer
  • 14,942
  • 6
  • 61
  • 99