Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a kind name independently of the model name (App Engine datastore)

As a Python programmer, I like my code to be reusable, I'm trying to avoid kind name conflicts in my code (where two different models share the same kind name).

Currently I just prepend some meaningful text to the model's class name, but this is awfully unpythonic.

Being able to explicitly set the model's kind will solve my problem, but I can't find out how to do this, does anyone know how?

like image 547
Noah McIlraith Avatar asked Dec 14 '25 12:12

Noah McIlraith


1 Answers

Just override the kind() method of your class:

class MyModel(db.Model):
  @classmethod
  def kind(cls):
    return 'prefix_%s' % super(MyModel, cls).kind()

You can define a custom baseclass that does this for you:

class ModuleModel(db.Model):
  @classmethod
  def kind(cls):
    return '%s_%s' % (cls.__module__, super(ModuleModel, cls).kind())

Any class that extends ModuleModel will have the name of the module it's defined in prefixed to the kind name.

like image 111
Nick Johnson Avatar answered Dec 18 '25 13:12

Nick Johnson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!