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

Reload doesn't check if attribute is writable #1276

Open
xelhark opened this issue Apr 5, 2016 · 3 comments
Open

Reload doesn't check if attribute is writable #1276

xelhark opened this issue Apr 5, 2016 · 3 comments
Labels

Comments

@xelhark
Copy link

xelhark commented Apr 5, 2016

Hello!

I noticed a weird behavior and managed to isolate the problem.

I think this situation is not as uncommon as it might appear:

from mongoengine import Document, IntField, connect

__author__ = 'gabriele'

connect('test_mongoengine')


class MyModel(Document):
    meta = {
        'strict': False
    }

    int_field = IntField()
    deleted_field = IntField()


new_model = MyModel(int_field=3, deleted_field=4)
new_model.save()


class MyModel(Document):
    int_field = IntField()

    meta = {
        'strict': False
    }

    @property
    def deleted_field(self):
        return 'default'


obj = MyModel.objects(int_field=3).first()

obj.reload()

This will cause mongoengine to raise an AttributeError :

[...]/mongoengine/base/document.py", line 187, in __setattr__
    super(BaseDocument, self).__setattr__(name, value)
AttributeError: can't set attribute

This is an inconsistent behavior because the error is NOT raised when the object is first loaded, but when it is reloaded.

@lafrech lafrech added the Bug label Apr 5, 2016
@lafrech
Copy link
Member

lafrech commented Apr 5, 2016

Duplicate of #1259?

@amcgregor
Copy link
Contributor

amcgregor commented Jun 23, 2016

@lafrech Related, but doesn't seem to be a duplicate. These are two very different problems, #1259 with reloading something that actually is still a field, this one the fact that the field attribute has been overridden with a non-field that can't be written to.

@amcgregor
Copy link
Contributor

Looking into this a bit further, things are even stranger. After obj = MyModel.objects(int_field=3).first(), the following do give correct answers:

MyModel._fields
MyModel._fields_ordered
obj._fields
obj._fields_ordred

The reload code is inappropriately examining _data instead of the actual field lists to determine which attributes to save back out (to trigger the descriptor protocol on those fields).

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

No branches or pull requests

3 participants