Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to define metaclass for a class that extends from sqlalchemy declarative base
    text
    copied!<p>I use: Python 2.6 and sqlalchemy 0.6.1</p> <p>This is what I am trying to do:</p> <pre><code>from sqlalchemy.types import ( Integer, String, Boolean ) from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class SampleMeta(type): def __new__(cls, name, bases, attrs): attrs.update({ 'id': Column('Id', Integer, primary_key=True), 'name': Column('Name', String), 'description': Column('Description', String), 'is_active': Column('IsActive', Boolean) }) return super(SampleMeta, cls).__new__(cls, name, bases, attrs) class Sample(Base): __tablename__ = 'Sample' __table_args__ = {'useexisting': True} __metaclass__ = SampleMeta def __init__(self, id, name, description, is_active): self.id = id self.name = name self.description = description self.is_active = is_active def __repr__(self): return "&lt;(%d, '%s', '%s', %r)&gt;" % (self.id, self.name, self.description, self.isactive) </code></pre> <p>And the error I am getting is this:</p> <pre><code>TypeError: Error when calling the metaclass bases metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases </code></pre> <p>Now, if I do the same thing above by using</p> <pre><code>class Sample(object) </code></pre> <p>instead of</p> <pre><code>class Sample(Base) </code></pre> <p>it works absolutely fine.</p> <p>I need to update the attributes of the class dynamically. So, I will be using dynamic attribute and column names. And I need the above piece code to work in order to be able to get there.</p> <p><strong>Please help</strong> </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload