PynamoDB icon indicating copy to clipboard operation
PynamoDB copied to clipboard

Adding support for abstract inheritance in Pynamo

Open betamoo opened this issue 7 years ago • 4 comments

Changes are described in https://github.com/pynamodb/PynamoDB/issues/439

betamoo avatar Jan 29 '18 20:01 betamoo

I had a thought with what might be an alternate approach (though you all might have already considered and rejected this):

Given that we already have AttributeContainer as a base class for inheriting MapAttributes, and that a Model already extends it, do we perhaps already have a form of abstract inheritance already and we simply need to document / codify the rules around it?

SharedAttributes(AttributeContainer):
    foo = StringAttribute(hash_key=True)

MyModelWithBar(Model, SharedAttributes):
    # inherits foo from SharedAttributes
    bar = NumberAttribute()

MyModelWithBaz(Model, SharedAttribtues):
    # inherits foo from SharedAttributes
    baz = NumberAttribtue()

jpinner-lyft avatar Feb 05 '18 17:02 jpinner-lyft

How is this PR going?

bendog avatar Apr 18 '19 04:04 bendog

@garrettheel Can you review it please?

mojimi avatar May 13 '19 19:05 mojimi

@jpinner-lyft @garrettheel any chance we can inherit from pynamodb.models.Model in a BaseModel that we can inherit from in all our custom models? That way it would be possible to write functionality in tandem with Model functionality. For example, right now if I want to write some code for the save function before doing super(...).save() I have to do it for every model and that is not very DRY. Any chance that this is supported in any way?

The abstract class implementation in the PR above sounds like a good solution for my use case. I already tried just doing it with pynamodb out of the box but receive an error about the constructor not getting the right arguments. Any ideas or suggestions?

marioscience avatar May 01 '21 21:05 marioscience