SQLAlchemy mixin in method

Created: September 25, 2019  |  Updated: July 26, 2020  |  less than 1 minute read

If I’m not wrong, the SQLAlchemy official doc provides some examples to explain how to share a set of common columns, some common table options, or other mapped properties, across many classes. But I cannot find how to share common methods (e.g. your customized to_dict() method). This post will just show you a POC to achieve this goal by using Python Mixin.

Share the common method to_dict() across two SQLAlchemy models

from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class ModelMixin(object):

    def to_dict(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}


class ModelA(Base, ModelMixin):
    __tablename__ = "model_a"

    model_id = Column(Integer, primary_key=True)
    name = Column(String)


class ModelB(Base, ModelMixin):
    __tablename__ = "model_b"

    model_id = Column(Integer, primary_key=True)
    name = Column(String)

Test:

# to_dict() method from ModelMixin is shared between ModelA and ModelB

>>> a = ModelA(model_id=11, name='a1')
>>> a.to_dict()
{'model_id': 11, 'name': 'a1'}

>>> b = ModelB(model_id=22, name='b1')
>>> b.to_dict()
{'model_id': 22, 'name': 'b1'}

Leave a comment