I am finding myself doing a fair amount of copy-and-paste in the data model I'm currently working on, and I'd like to reduce that if possible. For example, I have several different types of objects that have names belonging to namespaces. So every such table gets boilerplate looking like this (using declarative):
namespace_id = Column(Integer, ForeignKey(Namespace.id), nullable=False) namespace = relationship(Namespace, backref='widgets') name = Column(Unicode(256), nullable=False) __table_args__ = ( UniqueConstraint(namespace_id, name), {}) In the above example, the only thing which changes from class to class is the backref='widgets' part. As another example, I would like to be able to attach notes to various kinds of objects, and since the objects are not related by inheritance, I think the best way is to create a separate notes table for each class of object. So for example a Widget class (mapped to widgets table) would lead to a WidgetNote class (mapped to widget_notes). (The alternative, having a single notes table, requires all notable objects to be related by polymorphic inheritance, which as I said isn't really a great idea in my case.) Ideally, I could just stick a decorator on the Widget class and have the notes handled automagically. I am thinking that metaclasses are the appropriate thing here. I have never delved deeply into metaclasses, though, and I know that SqlAlchemy is already doing a lot of metaclass magic, and I worry that I may screw something up. And I really need several different types of metaclasses that can be combined, so I need some kind of metaclass mix-in, and that all sounds very disaster-prone. Am I barking up the wrong tree here? Can anybody point me to some examples of metaclasses being used for something like this? -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.