Skip to content

Cast field value upon assignment #1033

Open
@warvariuc

Description

@warvariuc

When you assign a value to a model instance field, the value is not immediately converted to the type the field should hold:

ipdb> thread.__class__.tutor_id
<mongoengine.base.fields.ObjectIdField object at 0x7f30443c3f50>
ipdb> thread.tutor_id = '558143a3bb16fb194ff98526'
ipdb> thread.tutor_id
'558143a3bb16fb194ff98526'
ipdb> thread.student_id
ObjectId('558143a3bb16fb194ff98522')

This leads sometimes to bugs which are difficult to discover. In the case above thread.tutor_id was assigned an id which came from a POST request. Then later in the code I am using thread.tutor_id as in a query filter, which fails, because 558143a3bb16fb194ff98526 is not the same as ObjectId('558143a3bb16fb194ff98526').

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions