minou
minou

Reputation: 16563

GAE: Convert model to subclass of polymodel

I have an existing GAE app with a reasonably small number of entities, and I would like to update the entities to use polymodel.

I currently have entities like this:

class Mammal(db.Model)
class Reptile(db.Model)

and I'd like to change it to this:

class Animal(polymodel.Polymodel)
class Mammal(Animal)
class Reptile(Animal)

My current plan is to do the following procedure:

  1. Iterate over all of the existing entities to change the class names to some temporary class name. E.g., convert class Mammal(db.Model) to class MammalTmp(db.Model) and convert class Reptile(db.Model) to class ReptileTmp(db.Model). In doing this, I would copy all of the properties of the old class to the new class.

  2. Delete all instances of class Mammal(db.Model) and class Reptile(db.Model).

  3. Iterate over all of the temporary entities to change the class names to the desired class name and type. E.g., convert class MammalTmp(db.Model) to class Mammal(polymodel.Polymodel) and convert class ReptileTmp(db.Model) to class Reptile(polymodel.Polymodel). I would again copy all of the properties of the old class to the new class.

  4. Delete all instances of class MammalTmp(db.Model) and class ReptileTmp(db.Model).

This is a laborious procedure! Is there an easier way to accomplish this?

Upvotes: 1

Views: 182

Answers (2)

cod3monk3y
cod3monk3y

Reputation: 9843

I'm not familiar at all with GAE, but could you just redirect your model definitions through an intermediary? I suppose this wouldn't be any faster than just renaming the base class for all your models, though.

Create a redirect class to start with:

# redirect.py
# note: I don't really know where db comes from...
import gae.db as db

class Model(db.Model):
    pass

Then add this line to your model file:

import redirect as db
class Mammal(db.Model):
    pass

And since db is now the redirect version, you can change the redirect file..

class Model (db.PolyModel):
    pass

But now that I've written it, it sounds like just as much work as manually updating the files, and you lose all access to db. for other basic operations. So, maybe I should just downvote my own answer. :D

Upvotes: 0

Patrice
Patrice

Reputation: 4692

With the way entities are built and then indexed, no unfortunately there is no other way (as far as I know) to do a similar process. I had to do it when I first wanted to implement polymodels and that's the way I did it. Luckily all of these can be done through code so you don't really have to sit at your computer and change all of that manually.

It's lengthy for sure, but think of all the speed benefits the datastore offers. That's why you have to be careful about creating your models in the first place. I know it's not necessarily easy (as I said I fell in the same hole as you and had to write code for those iterations and changes).

A very good way to do such a process programmatically would be to use MapReduce. A "mapper" could definitely do the trick and help you do that faster and more efficiently. Looking into the sample projects might give you some pointers.

Upvotes: 2

Related Questions