MongoEngine / MongoEngine/mongoengine
Overriden `prepare_query_value` breaks current SequenceField behavior
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.3k
- Forks
- 1.2k
- Avg merge
- 4h 41m
- Merged PRs (30d)
- 11
Description
I have a class with sequence field defined like this:
class A(Document):
id = me.SequenceField(
primary_key=True,
value_decorator='{:x}'.format # to hex
)
In mongoengine 0.8 it worked like this --- sequence field produced new integer and value_decorator converted it to hex string and it was stored in database like this. And this converted hex string value was used everywhere. "Raw" integer sequence value was used only for generating new id.
A.objects.get(id='1ab')
In version 0.9 if i try to get object of class A like this I will get an error:
...
File ".../lib/python2.7/site-packages/mongoengine/queryset/transform.py", line 94, in query
value = field.prepare_query_value(op, value)
File ".../lib/python2.7/site-packages/mongoengine/fields.py", line 1784, in prepare_query_value
return self.value_decorator(value)
ValueError: Unknown format code 'x' for object of type 'unicode'
I understand what was the logic behind this. For default value_decorator value for SequenceField can be passed as both string ('10') or integer (10), but it doesn't work for specific decorators like these.
I don't know what is the best course of action: just don't use override if self.value_decorator is not equal to self.VALUE_DECORATOR or create specific option like "decorate_on_query". I'm asking an advice on that and then I'd like to create a pull request.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the SequenceField implementation in mongoengine/fields.py, especially prepare_query_value, and trace how query values are transformed through mongoengine/queryset/transform.py. Reproduce the custom hexadecimal value_decorator case, then add regression coverage showing that querying a decorated sequence value works without breaking the default behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- mongodb, python
- Domain
- database
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100