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

Method switch_collection

mongoengine/document.py:725–752  ·  view source on GitHub ↗

Temporarily switch the collection for a document instance. Only really useful for archiving off data and calling `save()`:: user = User.objects.get(id=user_id) user.switch_collection('old-users') user.save() :param str collection_name:

(self, collection_name, keep_created=True)

Source from the content-addressed store, hash-verified

723 return self
724
725 def switch_collection(self, collection_name, keep_created=True):
726 """
727 Temporarily switch the collection for a document instance.
728
729 Only really useful for archiving off data and calling `save()`::
730
731 user = User.objects.get(id=user_id)
732 user.switch_collection('old-users')
733 user.save()
734
735 :param str collection_name: The database alias to use for saving the
736 document
737
738 :param bool keep_created: keep self._created value after switching collection, else is reset to True
739
740
741 .. seealso::
742 Use :class:`~mongoengine.context_managers.switch_db`
743 if you need to read from another database
744 """
745 with switch_collection(self.__class__, collection_name) as cls:
746 collection = cls._get_collection()
747 self._get_collection = lambda: collection
748 self._collection = collection
749 self._created = True if not keep_created else self._created
750 self.__objects = self._qs
751 self.__objects._collection_obj = collection
752 return self
753
754 def select_related(self, max_depth=1):
755 """Handles dereferencing of :class:`~bson.dbref.DBRef` objects to

Calls 2

switch_collectionClass · 0.90
_get_collectionMethod · 0.80

Tested by 1