Mongoengine: Queryset update doesn't go through field validation

Created on 22 Aug 2013  路  6Comments  路  Source: MongoEngine/mongoengine

Here's an example:

import mongoengine as me
me.connect('test')

class AgeField(me.IntField):
    def validate(self, value):
        if value < 0:
            self.error('must be larger than zero')

class Person(me.Document):
    age = AgeField()

try:
    Person(age=-2).save()
except me.ValidationError:
    pass
else:
    1/0
p = Person(age=100).save()
Person.objects.update(set__age=-1)
print Person.objects()[0].age # prints -1

I found a nice solution for this, not sure if it's right though:

    def prepare_query_value(self, op, value):
        self.validate(value)
        return super(AgeField, self).prepare_query_value(op, value)

Most helpful comment

Hi, the fix doesn't seem to fix the issue of no signals on update(modify, update_one also). I can't seem to find anyway to do atomic updates and have signals without it???

This is a very big problem. Till it is fixed, I hope maintainers can mention this issue in the docs. After update, I called save then reload but data is returned without any validation error. This should be Documented, at least.

All 6 comments

As this is a breaking change - I'm marking this down for 0.9

What is the status of this? Validators not being run means that also the regex-validation is not run which is very annoying

Any updates or plans for this one? This will be very useful. Thanks!

Just an FYI - calling update() not only skips validation, but does not throw any of the related signals.

Hi, the fix doesn't seem to fix the issue of no signals on update(modify, update_one also). I can't seem to find anyway to do atomic updates and have signals without it???

Hi, the fix doesn't seem to fix the issue of no signals on update(modify, update_one also). I can't seem to find anyway to do atomic updates and have signals without it???

This is a very big problem. Till it is fixed, I hope maintainers can mention this issue in the docs. After update, I called save then reload but data is returned without any validation error. This should be Documented, at least.

Was this page helpful?
0 / 5 - 0 ratings