but seriously, thank you so much! This works perfectly, so if you want to copy that over to stackoverflow, I can select that as the correct answer (if you want the updoots).
Final thing: Something about that code feels unpythonic to me, but maybe I am just not used to this sort of work. Would there be some way of being more verbose in the main class so that I could get the prompt in Pycharm for setting the defaults? If you don't have the time, don't even worry about it, I'll be working on this a little bit, but thanks for all the help Mike! Marcus On Sun, Jul 1, 2018 at 10:27 AM, Mike Bayer <[email protected]> wrote: > On Sun, Jul 1, 2018 at 9:15 AM, Marcus Mann <[email protected]> wrote: > > So not a property of the column necessarily, but rather the actual value > of > > the column. Like if there is a column that stores the Name of the Item, > > there would be a "Name" column. I would like to change it so when you > make > > an A() object, it uses the default value of "A" in the "Name" column, and > > when you create a 'B' object, it uses the default value of "B" in the > "Name" > > Column. > > class specific defaults at the ORM level are most easily set up just > by treating the class like any other: > > class A(Base): > __tablename__ = ... > > col1 = Column(..) > col2 = Column(...) > > def __init__(self, **kw): > kw.setdefault("col2", "this is an A") > super(A, self).__init__(**kw) > > > class B(A): > def __init__(self, **kw): > kw.setdefault("col2", "this is a B") > super(B, self).__init__(**kw) > > if using __init__ feels wrong, there's also an init event, which I > would advise setting only on the base class as it's not easy to have > multiple listeners fire off in an exact ordering > (http://docs.sqlalchemy.org/en/latest/orm/events.html? > highlight=instanceevents%20init#sqlalchemy.orm.events.InstanceEvents.init > ): > > from sqlalchemy import event > > class A(Base): > __tablename__ = ... > > col1 = Column(..) > col2 = Column(...) > col2_default = "This is an A" > > > class B(A): > col2_default = "this is a B" > > @event.listens_for(A, 'init', propagate=True) > def _init_a(target, args, kwargs): > kwargs.setdefault("col2", target.col2_default) > > > > > > > > -- > > SQLAlchemy - > > The Python SQL Toolkit and Object Relational Mapper > > > > http://www.sqlalchemy.org/ > > > > To post example code, please provide an MCVE: Minimal, Complete, and > > Verifiable Example. See http://stackoverflow.com/help/mcve for a full > > description. > > --- > > You received this message because you are subscribed to the Google Groups > > "sqlalchemy" group. > > To unsubscribe from this group and stop receiving emails from it, send an > > email to [email protected]. > > To post to this group, send email to [email protected]. > > Visit this group at https://groups.google.com/group/sqlalchemy. > > For more options, visit https://groups.google.com/d/optout. > > -- > SQLAlchemy - > The Python SQL Toolkit and Object Relational Mapper > > http://www.sqlalchemy.org/ > > To post example code, please provide an MCVE: Minimal, Complete, and > Verifiable Example. See http://stackoverflow.com/help/mcve for a full > description. > --- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at https://groups.google.com/group/sqlalchemy. > For more options, visit https://groups.google.com/d/optout. > -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
