Question

I'm making a spam filter library for google app engine, which uses app engine hooks to modify some data right before it is put() into the datastore. To do so, I created a model that subclasses the PolyModel class, which classes wishing to be checkable should subclass.

But for some data in the class, I'd like to do some stuff when the value is changed, for which I'd have to overwrite __setitem__. But I believe the sdk uses this to determine changes in the properties of models, apply filters etc, and so changing overwriting __setitem__ might break this. What's the best way around this?

Was it helpful?

Solution

There is no danger in overriding methods, if you don't forget to call the parent method with super. But in your case __setitem__ is wrong place, because then everytime when instance is created it will be cleaned from spam, even if it is created from already cleaned values.

What is wrong if you do that explicitly?

filter_out_spam(obj)
obj.put()

OTHER TIPS

Don't modify __setitem__ - write a custom property class that does what you want, instead.

Bear in mind that you can't just override put on your model if you want to do things before the record is put, because entities can also be stored with db.put. If you can, you should avoid the need for this by moving the relevant code into property classes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top