Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DictField _auto_dereference gets changed after first query #2831

Open
bagerard opened this issue Aug 21, 2024 · 0 comments
Open

DictField _auto_dereference gets changed after first query #2831

bagerard opened this issue Aug 21, 2024 · 0 comments

Comments

@bagerard
Copy link
Collaborator

bagerard commented Aug 21, 2024

The _auto_dereference flag of DictField defaults to False, but it gets overwritten to True (permanently) after any Doc gets loading through a Queryset

    def test_dictfield_dump_document_with_inheritance__cls(self):
        """Ensure a DictField can handle another document's dump."""

        class Doc(Document):
            field = DictField()

        class ToEmbedParent(Document):
            id = IntField(primary_key=True)
            recursive = DictField()

            meta = {"allow_inheritance": True}

        class ToEmbedChild(ToEmbedParent):
            def __init__(self, *args, **kwargs):
                super().__init__(*args, **kwargs)

        Doc.drop_collection()
        ToEmbedParent.drop_collection()

        # with a Document with a _cls field
        to_embed_recursive = ToEmbedChild(id=1).save()
        to_embed_child = ToEmbedChild(
            id=2, recursive=to_embed_recursive.to_mongo().to_dict()
        ).save()

        doc_dump_as_dict = to_embed_child.to_mongo().to_dict()
        doc = Doc(field=doc_dump_as_dict)
        assert Doc.field._auto_dereference is False
        assert isinstance(doc.field, dict)  # depends on auto_dereference
        doc.save()
        assert isinstance(doc.field, dict)
        expected = {
            "_id": 2,
            "_cls": "ToEmbedParent.ToEmbedChild",
            "recursive": {
                "_id": 1,
                "_cls": "ToEmbedParent.ToEmbedChild",
                "recursive": {},
            },
        }
        assert doc.field == expected

       # Now after querying
        _ = Doc.objects.first()
        assert Doc.field._auto_dereference is False   # Fails
        doc = Doc(field=doc_dump_as_dict)
        assert isinstance(doc.field, dict)                       # Fails 

This has consequence on how it loads an EmbeddedDocument with inheritance afterward.
It is due to this part , which mutates the original field's auto_deref flag for DictField (not only in case of copies of the Fields as it should)

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant