On Sep 18, 2008, at 6:59 AM, GHZ wrote:

>
> I want to set table, mapper arguments automatically.
> The following is the only way I have found to do this.
> Is this supported?
> Am I wasting my time with Declarative and should rather use the non
> declarative if I want this control?
>
> class MyMeta(DeclarativeMeta):
>
>    def __new__(meta, classname, bases, classdict):
>
>        # Copy interesting arguments from base classes
>        for base in bases:
>            for arg in (a for a in ('__table_args__',
>                                    '__tablename__',
>                                    '__mapper_args__') if a in
> base.__dict__):
>                classdict[arg] = base.__dict__[arg]
>
>        return DeclarativeMeta.__new__(meta, classname, bases,
> classdict)
>
> Base = declarative_base(bind=engine)
>
> class MyDeclarativeStuff(object):
>    # Set some generic stuff up.
>    __table_args__ = {'autoload':True}
>
> class Customer(Base, MyDeclarativeStuff):
>    __metaclass__ = MyMeta
>    __tablename__ = 'customer'


Well, I would think "__table_args__" is the only argument you'd really  
want to propigate in that way, and this is an inconvenience I've also  
had so perhaps we'll do something about it...I would propose a  
"default_table_args" keyword arg to declarative_base().

As for mapper args, I'd just supply a mapper callable to  
declarative_base() which sets up the desired **kwargs (like  
extensions), and returns a mapper() from that (its an available kwarg).

Check out the **kwargs for declarative_base, as it also takes a  
"metaclass" argument so you wouldn't need to jump through the hoops  
you're jumping.  Also, the individual components within  
declarative_base() are availalble as separate functions, so you could  
build a recipe like the above more directly.  Check out the source.





--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to