0

スーパークラスで宣言されたメタクラスから継承するクラスを作成しようとしました。

from sqlalchemy import Column, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker


class Database(object):
    """
        Class that holds all data models classes and functions
    """
    def __init__(self, wenDbPath = "test.db"):
        self.engine = create_engine('sqlite:///' + "c:/"+wenDbPath, echo=False)
        self.Session = sessionmaker(bind=self.engine)
        **self.Base** = declarative_base() # This returns a metaclass.
        self.Base.metadata.create_all(self.engine)
        self.session = self.Session()

    class Element(**self.Base**):
        __tablename__ = 'elements'
        idNumber = Column(String(255), primary_key=True)
        smallDiscription = Column(String(50), nullable=False)
        longDiscription = Column(String())
        webLink = Column(String())

        def __init__(self, idNumber, smallDiscription, longDiscription, webLink):
            self.idNumber = idNumber
            self.longDiscription = longDiscription
            self.smallDiscription = smallDiscription
            self.webLink = webLink
        def __repr__(self):
            return "<Element ('%s : %s')>" % (self.idNumber, self.smallDiscription)

Python から次のメッセージが表示されます

class Element(self.Base): NameError: 名前 'self' が定義されていません

どうすればこのようなことができますか?

前もって感謝します。

4

1 に答える 1