mlsteeves
mlsteeves

Reputation: 1271

How to change the default property on a set in python and Google AppEngine

In the following code:

class ClassA(db.Model):
    name = db.StringProperty()

class ClassB(db.Model):
    name = db.StringProperty()
    deleted_flag = db.BooleanProperty()
    classA = db.ReferenceProperty(ClassA)

ClassA will have a property called classb_set. When I call classb_set within code, I do not want it to return items that have the deleted_flag = true.

How can I change the default filter on the classb_set query? My first instinct is to create another method in ClassA that does this filter, but is there a way that keeps the classb_set a property?

Upvotes: 0

Views: 152

Answers (1)

Adam Crossland
Adam Crossland

Reputation: 14213

You can always use a Python property to accomplish your goal:

class ClassA(db.Model):
    name = db.StringProperty()

    def __get_classBdeleted(self):
        return self.classB_set.filter('deleted_flag =', 'True')

    classBdeleted = property(__get_classBdeleted)

class ClassB(db.Model):
    name = db.StringProperty()
    deleted_flag = db.BooleanProperty()
    classA = db.ReferenceProperty(ClassA)

Upvotes: 4

Related Questions