MongoEngine/mongoengine — pull request #143 by slagle (2012-09-28 17:10:32+00:00)
Title: Document subclasses with a different db_alias setting from their base class were not using the correct db_alias

When subclassing a Document class, if you overrode the db_alias setting in the subclass, the setting was not taking effect if an operation had already been done that set the _collection attribute on the base class.

The subclass was re-using the _collection object from the base class which is pointed at the wrong db. This gist illustrates the issue: https://gist.github.com/3801013 You'll notice the SubUser that is created is saved to the db with the default alias (db1 in this case), and not db2.

This commit (with test case) resolves the issue by setting the _collection attribute to None (forcing a refresh) if the name of the database that is set does not match what is expected.