Changed the inheritance model to remove types
The inheritance model has changed, we no longer need to store an array of `types` with the model we can just use the classname in `_cls`. See the upgrade docs for information on how to upgrade MongoEngine/mongoengine#148
This commit is contained in:
@@ -2,6 +2,11 @@
|
||||
Changelog
|
||||
=========
|
||||
|
||||
Changes in 0.8
|
||||
==============
|
||||
- Remove _types and just use _cls for inheritance (MongoEngine/mongoengine#148)
|
||||
|
||||
|
||||
Changes in 0.7.X
|
||||
================
|
||||
- Unicode fix for repr (MongoEngine/mongoengine#133)
|
||||
|
||||
@@ -461,9 +461,10 @@ If a dictionary is passed then the following options are available:
|
||||
:attr:`fields` (Default: None)
|
||||
The fields to index. Specified in the same format as described above.
|
||||
|
||||
:attr:`types` (Default: True)
|
||||
Whether the index should have the :attr:`_types` field added automatically
|
||||
to the start of the index.
|
||||
:attr:`cls` (Default: True)
|
||||
If you have polymorphic models that inherit and have `allow_inheritance`
|
||||
turned on, you can configure whether the index should have the
|
||||
:attr:`_cls` field added automatically to the start of the index.
|
||||
|
||||
:attr:`sparse` (Default: False)
|
||||
Whether the index should be sparse.
|
||||
@@ -590,14 +591,14 @@ convenient and efficient retrieval of related documents::
|
||||
Working with existing data
|
||||
--------------------------
|
||||
To enable correct retrieval of documents involved in this kind of heirarchy,
|
||||
two extra attributes are stored on each document in the database: :attr:`_cls`
|
||||
and :attr:`_types`. These are hidden from the user through the MongoEngine
|
||||
interface, but may not be present if you are trying to use MongoEngine with
|
||||
an existing database. For this reason, you may disable this inheritance
|
||||
mechansim, removing the dependency of :attr:`_cls` and :attr:`_types`, enabling
|
||||
you to work with existing databases. To disable inheritance on a document
|
||||
class, set :attr:`allow_inheritance` to ``False`` in the :attr:`meta`
|
||||
dictionary::
|
||||
an extra attribute is stored on each document in the database: :attr:`_cls`.
|
||||
These are hidden from the user through the MongoEngine interface, but may not
|
||||
be present if you are trying to use MongoEngine with an existing database.
|
||||
|
||||
For this reason, you may disable this inheritance mechansim, removing the
|
||||
dependency of :attr:`_cls`, enabling you to work with existing databases.
|
||||
To disable inheritance on a document class, set :attr:`allow_inheritance` to
|
||||
``False`` in the :attr:`meta` dictionary::
|
||||
|
||||
# Will work with data in an existing collection named 'cmsPage'
|
||||
class Page(Document):
|
||||
|
||||
@@ -2,6 +2,45 @@
|
||||
Upgrading
|
||||
=========
|
||||
|
||||
0.7 to 0.8
|
||||
==========
|
||||
|
||||
Inheritance
|
||||
-----------
|
||||
|
||||
The inheritance model has changed, we no longer need to store an array of
|
||||
`types` with the model we can just use the classname in `_cls`. This means
|
||||
that you will have to update your indexes for each of your inherited classes
|
||||
like so:
|
||||
|
||||
# 1. Declaration of the class
|
||||
class Animal(Document):
|
||||
name = StringField()
|
||||
meta = {
|
||||
'allow_inheritance': True,
|
||||
'indexes': ['name']
|
||||
}
|
||||
|
||||
# 2. Remove _types
|
||||
collection = Animal._get_collection()
|
||||
collection.update({}, {"$unset": {"_types": 1}}, multi=True)
|
||||
|
||||
# 3. Confirm extra data is removed
|
||||
count = collection.find({'_types': {"$exists": True}}).count()
|
||||
assert count == 0
|
||||
|
||||
# 4. Remove indexes
|
||||
info = collection.index_information()
|
||||
indexes_to_drop = [key for key, value in info.iteritems()
|
||||
if '_types' in dict(value['key'])]
|
||||
for index in indexes_to_drop:
|
||||
collection.drop_index(index)
|
||||
|
||||
# 5. Recreate indexes
|
||||
Animal.objects._ensure_indexes()
|
||||
|
||||
|
||||
|
||||
0.6 to 0.7
|
||||
==========
|
||||
|
||||
|
||||
Reference in New Issue
Block a user