MCPcopy Index your code
hub / github.com/MongoEngine/mongoengine / switch_db

Method switch_db

mongoengine/document.py:695–723  ·  view source on GitHub ↗

Temporarily switch the database for a document instance. Only really useful for archiving off data and calling `save()`:: user = User.objects.get(id=user_id) user.switch_db('archive-db') user.save() :param str db_alias: The database ali

(self, db_alias, keep_created=True)

Source from the content-addressed store, hash-verified

693 signals.post_delete.send(self.__class__, document=self, **signal_kwargs)
694
695 def switch_db(self, db_alias, keep_created=True):
696 """
697 Temporarily switch the database for a document instance.
698
699 Only really useful for archiving off data and calling `save()`::
700
701 user = User.objects.get(id=user_id)
702 user.switch_db('archive-db')
703 user.save()
704
705 :param str db_alias: The database alias to use for saving the document
706
707 :param bool keep_created: keep self._created value after switching db, else is reset to True
708
709
710 .. seealso::
711 Use :class:`~mongoengine.context_managers.switch_collection`
712 if you need to read from another collection
713 """
714 with switch_db(self.__class__, db_alias) as cls:
715 collection = cls._get_collection()
716 db = cls._get_db()
717 self._get_collection = lambda: collection
718 self._get_db = lambda: db
719 self._collection = collection
720 self._created = True if not keep_created else self._created
721 self.__objects = self._qs
722 self.__objects._collection_obj = collection
723 return self
724
725 def switch_collection(self, collection_name, keep_created=True):
726 """

Callers 3

test_usingMethod · 0.80

Calls 3

switch_dbClass · 0.90
_get_collectionMethod · 0.80
_get_dbMethod · 0.80

Tested by 3

test_usingMethod · 0.64