473,322 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How to define metaclass for a class that extends from sqlalchemy declarative base

15
I use: Python 2.6 and sqlalchemy 0.6.1

This is what I am trying to do:

Expand|Select|Wrap|Line Numbers
  1. from sqlalchemy.types import (
  2.         Integer,
  3.         String,
  4.         Boolean
  5.     )
  6.     from sqlalchemy.ext.declarative import declarative_base
  7.  
  8.     Base = declarative_base()
  9.  
  10.     class SampleMeta(type):
  11.         def __new__(cls, name, bases, attrs):
  12.             attrs.update({   'id': Column('Id', Integer, primary_key=True),
  13.                         'name': Column('Name', String),
  14.                         'description': Column('Description', String),
  15.                         'is_active': Column('IsActive', Boolean)
  16.                     })
  17.             return super(SampleMeta, cls).__new__(cls, name, bases, attrs)
  18.  
  19.     class Sample(Base):
  20.         __tablename__ = 'Sample'
  21.         __table_args__ = {'useexisting': True}
  22.         __metaclass__ = SampleMeta
  23.  
  24.         def __init__(self, id, name, description, is_active):
  25.             self.id = id
  26.             self.name = name
  27.             self.description = description
  28.             self.is_active = is_active
  29.  
  30.         def __repr__(self):
  31.             return "<(%d, '%s', '%s', %r)>" % (self.id, self.name, self.description, self.isactive)
And the error I am getting is this:

Expand|Select|Wrap|Line Numbers
  1. TypeError: Error when calling the metaclass bases
  2.     metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
  3.  
Now, if I do the same thing above by using

Expand|Select|Wrap|Line Numbers
  1. class Sample(object)
instead of

Expand|Select|Wrap|Line Numbers
  1. class Sample(Base)
it works absolutely fine.

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.

**Please help**
Jul 3 '13 #1
0 1130

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: saurabh | last post by:
solve a problem: define a Class(MyClassSArray a, b, c) which implements an array of string. But, in the backend it is actually implementing Linked List, so that memory is dynamically allocated....
10
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
0
by: Jonah Olsson | last post by:
Dear Sirs, I'm looking for a way to define a class using an XML file. Since I'm building a Web Service to be used by several customers I want to be able to easily change the class below without...
5
by: Rob | last post by:
In many articles related to VB.net the word "class" is used... How many meanings are there to this word ? "possible to derived a class from another" "forms are full-fledged classes" "base...
2
by: vilarneto | last post by:
Hello everyone, I'm facing a particular situation about template class derivation. The subject is old -- deriving a non-template class from a template base class -- but my problem is that the...
0
by: murl | last post by:
I want to create a web user control with a base class of 'GridView' so I can wrap the needed methods to make rows clickable. However when building the project I get the following message: "Make...
1
by: shofu_au | last post by:
Hi Group, I am trying to define a class that has a fixed size array of a structure containing a fixed size array of a structure. I am using System.Runtime.InteropServices and trying to define...
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
3
by: Immortal Nephi | last post by:
The rule of inheritance states that you define from top to bottom. Sometimes, you want to define base class and set reference from dervied class to base class, but you violate the rule. Here is an...
0
by: Peng Yu | last post by:
Hi, Suppose I have two namespaces A and B, I'm wondering if I can define a class in B for A. The question is relevant when I want to specialize some classes that have been defined in the std::...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.