I have a class:
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class DataFromWeb(Base):
__tablename__ = 'data'
id = Column(Integer, primary_key=True)
name = Column(String(50))
Now I'm getting from the web a JSON array which also have several other fields (in which obviously I'm not interested in, and could change but shouldn't break the app).
This is the error: TypeError: 'timestamp' is an invalid keyword argument for DataFromWeb. But there could be more fields. I'm just not interested in them.
Is there a nice way to ignore them? Or tell SQLAlchemy that the Columns I've given are the only ones I'm interested in?
One way I found is this:
def __init__(self, *args, **kwargs):
# Gotta clean up the kwargs here :(
kwargs2 = {k: v for k, v in kwargs.items() if hasattr(self.__class__, k)}
super().__init__(*args, **kwargs2)
But this is just ugly in my opinion...
Thanks!