On Mon, Jan 28, 2019 at 5:57 AM <dan.bar....@huawei.com> wrote: > > > Lets say I have a class > > class Dog(AlchemyBase): > __tablename__ = 'dogs' > name = Column(String, primary_key=True) > color = Column(String) > flees = relationship("Flee", backref="dogs") > > > > How can I get the list of fields ['name', 'color', 'flees'] from the class? > > I'd like to write a generic load(Dog, dict) method, that will create a Dog() > with the field values that exist in dict. > > e.g. > d = {'name: 'snoopy', 'junk': 1} > dog = load(Dog, d) > is equivalent to dog = Dog(name = 'snoopy') > > d1 = {'alias' : 'kitti'} > but load(Cat, d1) > is equivalent to cat = Cat(alias = 'kitti) > > Load will pick field names that exist in the passed Object, and have a value > in dict
the names you can get using inspect from sqlalchemy import inspect attr_names = inspect(Dog).attrs.keys() but a typical mapped class can take that dictionary directly in the constructor def load(cls, dict_): return cls(**dict_) is what you describe > > Thanks, > Dan > > -- > 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 sqlalchemy+unsubscr...@googlegroups.com. > To post to this group, send email to sqlalchemy@googlegroups.com. > 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 sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.