I'm writing a db.Model class in google app engine that looks something like this:
class Cheese(db.Model):
name = db.StringProperty()
def say_cheese(self):
return name + "cheese"
For some reason, whenever I run:
cheese = Cheese(name = "smelly")
print thing.say_cheese()
I get a KindError - No implementation for kind 'Cheese'. I want it to say: "smelly cheese"
Am I doing something wrong? Am I not allowed to add a method to a db.Model object?
It sounds like thing
is actually being loaded from a db.ReferenceProperty()
field (on a non-Cheese
entity) which happens to be referring to a Cheese
entity. If you access such a property without first importing the Cheese
model then the code won't be able to find the Cheese
kind to construct the entity and will fail with the error you indicated.
Anyway, try importing the Cheese
model in the code which is causing the error. Then the code should be able to find the implementation for Cheese
when it needs it.
To answer the other part of your question: Yes, you are certainly allowed to add your own methods to a db.Model
subclass.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With