Skip to content

Add a database router so dumpdata can ignore embedded models #259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions django_mongodb_backend/routers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.apps import apps

from django_mongodb_backend.models import EmbeddedModel


class MongoRouter:
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
EmbeddedModels don't have their own collection and must be ignored by
dumpdata.
"""
if not model_name:
return None
try:
model = apps.get_model(app_label, model_name)
except LookupError:
return None
return False if issubclass(model, EmbeddedModel) else None
7 changes: 7 additions & 0 deletions docs/source/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,10 @@ logging::
},
},
}

``dumpdata`` fails with ``CommandError: Unable to serialize database``
----------------------------------------------------------------------

If running ``manage.py dumpdata`` results in ``CommandError: Unable to
serialize database: 'EmbeddedModelManager' object has no attribute using'``,
see :ref:`configuring-database-routers-setting`.
15 changes: 15 additions & 0 deletions docs/source/intro/configure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,21 @@ it into the format above, you can use

This constructs a :setting:`DATABASES` setting equivalent to the first example.

.. _configuring-database-routers-setting:

Configuring the ``DATABASE_ROUTERS`` setting
============================================

If you intend to use :doc:`embedded models </topics/embedded-models>`, you must
configure the :setting:`DATABASE_ROUTERS` setting so that a collection for
these models isn't created and so that embedded models won't be treated as
normal models by :djadmin:`dumpdata`::

DATABASE_ROUTERS = ["django_mongodb_backend.routers.MongoRouter"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aclark4life
Can you make a change to the django-mongodb-project template to have this be a default setting?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


(If you've used the :djadmin:`startproject` template, this line is already
present.)

Congratulations, your project is ready to go!

.. seealso::
Expand Down
3 changes: 3 additions & 0 deletions docs/source/releases/5.0.x.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Django MongoDB Backend 5.0.x
- Added :doc:`async <django:topics/async>` support.
- Added the ``db_name`` parameter to
:func:`~django_mongodb_backend.utils.parse_uri`.
- Added ``django_mongodb_backend.routers.MongoRouter`` to allow
:djadmin:`dumpdata` to ignore embedded models. See
:ref:`configuring-database-routers-setting`.

5.0.0 beta 0
============
Expand Down
3 changes: 3 additions & 0 deletions docs/source/releases/5.1.x.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Django MongoDB Backend 5.1.x
- Added :doc:`async <django:topics/async>` support.
- Added the ``db_name`` parameter to
:func:`~django_mongodb_backend.utils.parse_uri`.
- Added ``django_mongodb_backend.routers.MongoRouter`` to allow
:djadmin:`dumpdata` to ignore embedded models. See
:ref:`configuring-database-routers-setting`.

5.1.0 beta 0
============
Expand Down
6 changes: 6 additions & 0 deletions tests/models_/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from django.db import models

from django_mongodb_backend.models import EmbeddedModel


class Embed(EmbeddedModel):
pass


class PlainModel(models.Model):
pass
20 changes: 20 additions & 0 deletions tests/models_/test_routers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.test import SimpleTestCase

from django_mongodb_backend.routers import MongoRouter


class TestRouter(SimpleTestCase):
def setUp(self):
self.router = MongoRouter()

def test_no_model(self):
self.assertIsNone(self.router.allow_migrate("db", "models_"))

def test_regular_model(self):
self.assertIsNone(self.router.allow_migrate("db", "models_", "plainmodel"))

def test_nonexistent_model(self):
self.assertIsNone(self.router.allow_migrate("db", "models_", "nonexistentmodel"))

def test_embedded_model(self):
self.assertIs(self.router.allow_migrate("db", "models_", "embed"), False)
Loading