Skip to main content

No project description provided

Project description

Django backend for MongoDB

Django backend for MongoDB.

Supports:

  • Column mappings to MongoDB documents
  • Single table (collection) inheritance and single table OneToOne relationships
  • Filters (filter/exclude)

Setup / Configuration

Not supported as primary database, as Django contrib apps rely on Integer primary keys in built in migrations (and because it is a use case that is not a priority at the moment).

# settings.py
DATABASES = {
    # or any other primary databse
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": BASE_DIR / "db.sqlite3",
    },
    # mongodb database, Client constructor options are passe in 'CLIENT', the database name in 'NAME'
    "mongodb": {
        "ENGINE": "django_mongodb",
        "NAME": "django_mongodb",
        "CONN_MAX_AGE": 120,
        "CLIENT": {
            "host": os.environ.get("MONGODB_URL"),
        },
    },
}
# A database is required
DATABASE_ROUTERS = ["testproject.router.DatabaseRouter"]

Using the database in models requires a DatabaseRouter, which could look like this

class DatabaseRouter:
    def db_for_read(self, model, **hints):
        if model._meta.app_label == "mymongoapp":
            return "default"
        return "default"

    def db_for_write(self, model, **hints):
        if model._meta.app_label == "mymongoapp":
            return "default"
        return "default"

    def allow_relation(self, obj1, obj2, **hints):
        if obj1._meta.app_label == obj2._meta.app_label:
            return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if app_label == "mymongoapp":
            # we are disabling migrations, as MongoDB is schema-less. Alerts, such as renaming fields, etc. are not supported
            return False
        return None

Finally we are going to change the default primary key of the app using MongoDB (if that is the case, otherwise add ObjectIdAutoField to the models, where you need it).

# apps.py
class TestappConfig(AppConfig):
    default_auto_field = "django_mongodb.models.ObjectIdAutoField"
    name = "mymongoapp"

Defining Models

A simple model, in an app, which has ObjectIdAutoField as default_auto_field

class MyModel(models.Model):
    json_field = JSONField()
    name = models.CharField(max_length=100)
    datetime_field = models.DateTimeField(auto_now_add=True)
    time_field = models.TimeField(auto_now_add=True)
    date_field = models.DateField(auto_now_add=True)

Single table inheritance

class SameTableChild(MyModel):
    my_model_ptr = models.OneToOneField(
        MyModel,
        on_delete=models.CASCADE,
        parent_link=True,
        related_name="same_table_child",
        # pointer to the primary key of the parent model
        db_column="_id",
    )
    extended = models.CharField(max_length=100)

    class Meta:
        # We are using the parent collection as db_table
        db_table = "mymongoapp_mymodel"

Single table OneToOne relationships

class SameTableOneToOne(models.Model):
    dummy_model = models.OneToOneField(
        MyModel,
        primary_key=True,
        on_delete=models.CASCADE,
        related_name="extends",
        db_column="_id",
    )
    extra = models.CharField(max_length=100)

    class Meta:
        # we are using the same collection to persist one-to-one relationships
        db_table = "mymongoapp_mymodel"

Querying

# get all objects
MyModel.objects.all()

# get all objects, which have a name in list ["foo", "bar"]
MyModel.objects.filter(name_in=["foo", "bar"])

# select related with single table inheritance and one to one relationships
MyModel.objects.select_related("same_table_child", "extends").all()

# simple aggregations
MyModel.objects.filter(name_in=["foo", "bar"]).count()

# raw mongo filter
MyModel.objects.filter(RawMongoDBQuery({"name": "1"})).delete()

Search

Using the prefer_search() extension of MongoQueryset, we can use the $search operator of MongoDB to query, if we have search indexes configured on the model.

MyModel.objects.prefer_search().filter(name="foo").all()

PostgreSQL search vectors map down to MongoDB search indexes, so we can use the same syntax as with PostgreSQL.

class MyModel(models.Model):
    name = models.CharField(max_length=100)
MyModel.objects.annotate(search=SearchVector('name')).filter(search=SearchQuery('foo')).all()

Raw Queries

with connections["mongodb"].cursor() as cursor:
    doc = cursor.collections["my_collection"].find_one()
    assert isinstance(doc["_id"], ObjectId)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_mongo_backend-0.27.5.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

django_mongo_backend-0.27.5-py3-none-any.whl (17.5 kB view details)

Uploaded Python 3

File details

Details for the file django_mongo_backend-0.27.5.tar.gz.

File metadata

  • Download URL: django_mongo_backend-0.27.5.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Darwin/23.2.0

File hashes

Hashes for django_mongo_backend-0.27.5.tar.gz
Algorithm Hash digest
SHA256 2621eb8a07720c5c5ef31a357c24b9c2dba75cb49c32c67360d27da9f11aec1b
MD5 f002eaedeee50462d6e2616990038112
BLAKE2b-256 55ae1f29f145fc4723bcb7f5168865c44d04857911ad4c468507e7b0c5766d40

See more details on using hashes here.

File details

Details for the file django_mongo_backend-0.27.5-py3-none-any.whl.

File metadata

File hashes

Hashes for django_mongo_backend-0.27.5-py3-none-any.whl
Algorithm Hash digest
SHA256 1042cfa9c451910b4371b7235a2e6cc1d0ff0b976272adb2e04820d04792a1ad
MD5 414af0d66dd486507174403203270b95
BLAKE2b-256 1e2ec28c1a66eed4e1c2815614b968615c54121417ab6c2d7d04b6c0beac3d02

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page