open up "declarative base" for more flexible subclassing

Issue #1042 resolved
Mike Bayer repo owner created an issue

ctheune wants a total standalone function that does what the metaclass does, so thats one thing. The other thing is, this should be easy:

class MyBase(declarative_base()):
    query = Session.query_property

    def __repr__(self):
        return <my custom repr whatever>

    def some_other_method(self):
        ...

the above might not work since it wants to build a table/mapper for MyBase. I'm also not thrilled with little "hints" like __no_map__=True and such since thats not really intuitive.

alternatively, you can just do it backwards with a mixin, but this is also not intuitive. We could just take the below and document it explicitly:

class _MyBase(object):
    query = Session.query_property

    def __repr__(self):
        return <my custom repr whatever>

    def some_other_method(self):
        ...

class MyBase(declarative_base(), _MyBase):
    ...

another idea:

class MyBase(object):
    query = Session.query_property

    def __repr__(self):
        return <my custom repr whatever>

    def some_other_method(self):
        ...
MyBase = declarative_base(cls=MyBase)

that might be the cleanest.

Comments (2)

  1. Log in to comment