Skip to content
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
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Development
- Fix .only() working improperly after using .count() of the same instance of QuerySet
- POTENTIAL BREAKING CHANGE: All result fields are now passed, including internal fields (_cls, _id) when using `QuerySet.as_pymongo` #1976
- Fix InvalidStringData error when using modify on a BinaryField #1127
- DEPRECATION: `EmbeddedDocument.save` & `.reload` are marked as deprecated and will be removed in a next version of mongoengine #1552

=================
Changes in 0.16.3
Expand Down
6 changes: 6 additions & 0 deletions mongoengine/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,15 @@ def to_mongo(self, *args, **kwargs):
return data

def save(self, *args, **kwargs):
warnings.warn("EmbeddedDocument.save is deprecated and will be removed in a next version of mongoengine."
"Use the parent document's .save() or ._instance.save()",
DeprecationWarning, stacklevel=2)
self._instance.save(*args, **kwargs)

def reload(self, *args, **kwargs):
warnings.warn("EmbeddedDocument.reload is deprecated and will be removed in a next version of mongoengine."
"Use the parent document's .reload() or ._instance.reload()",
DeprecationWarning, stacklevel=2)
self._instance.reload(*args, **kwargs)


Expand Down
24 changes: 21 additions & 3 deletions tests/document/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import weakref

from datetime import datetime

import warnings
from bson import DBRef, ObjectId
from pymongo.errors import DuplicateKeyError

Expand Down Expand Up @@ -1482,7 +1484,7 @@ class Message(Document):
Message.drop_collection()

# All objects share the same id, but each in a different collection
user = User(id=1, name='user-name')#.save()
user = User(id=1, name='user-name') # .save()
message = Message(id=1, author=user).save()

message.author.name = 'tutu'
Expand Down Expand Up @@ -2000,7 +2002,6 @@ class Record(Document):
child_record.delete()
self.assertEqual(Record.objects(name='parent').get().children, [])


def test_reverse_delete_rule_with_custom_id_field(self):
"""Ensure that a referenced document with custom primary key
is also deleted upon deletion.
Expand Down Expand Up @@ -3086,6 +3087,24 @@ def save(self, *args, **kwargs):
"UNDEFINED",
system.nodes["node"].parameters["param"].macros["test"].value)

def test_embedded_document_save_reload_warning(self):
"""Relates to #1570"""
class Embedded(EmbeddedDocument):
pass

class Doc(Document):
emb = EmbeddedDocumentField(Embedded)

doc = Doc(emb=Embedded()).save()
doc.emb.save() # Make sure its still working
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
with self.assertRaises(DeprecationWarning):
doc.emb.save()

with self.assertRaises(DeprecationWarning):
doc.emb.reload()

def test_embedded_document_equality(self):
class Test(Document):
field = StringField(required=True)
Expand Down Expand Up @@ -3381,7 +3400,6 @@ class Company(Document):
class User(Document):
company = ReferenceField(Company)


# Ensure index creation exception aren't swallowed (#1688)
with self.assertRaises(DuplicateKeyError):
User.objects().select_related()
Expand Down