No project description provided
Project description
Django backend for MongoDB (PoC Stage)
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django_mongo_backend-0.17.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b8a097dd4c684cb9a35e4619aa442d0bdc17791b2c184364bc371295f82d1c47 |
|
MD5 | 06dae75cf916771ed294bebdcff0fe7c |
|
BLAKE2b-256 | 1f1fb6dfcefb6b0bf5423bb941b695bde82cbcb5a88288c907a6c5a38689fe86 |
Hashes for django_mongo_backend-0.17-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 53429f029a34909776eb2b50988ed9b8022070d3d53995665fd6df8e79d41ebd |
|
MD5 | 24ee9c81b6762d3fdf9e732384a17879 |
|
BLAKE2b-256 | 6b4bf8f35f3f9daf4006778a3252dcf1320ffb3fa12e60cf34398583ebeb0290 |