MongoEngine / MongoEngine/mongoengine

Cast field value upon assignment

Open
#1,033 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement
Dominant language
Python
Stars
4.3k
Forks
1.2k
Avg merge
4h 41m
Merged PRs (30d)
11

Description

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').

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No files or tests are named. Start by tracing assignment to model instance fields and the ObjectIdField conversion path, then check how assigned values are later used in query filters. Done means values assigned to typed fields are converted consistently and the behavior is covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.