Skip to content

Overriden prepare_query_value breaks current SequenceField behavior #975

Open
@cachemem

Description

@cachemem

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.

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