0
I’m using Sqlalchemy to work with database tables. I am creating classes for the proper tables, where I have as methods of these classes, Insert, filter_all(), etc.
I’m using the following class with your methods:
class Veiculos(base):
__tablename__ = "veiculos"
id = Column(Integer, primary_key=True, autoincrement=True)
marca = Column(String(20))
modelo = Column(String(20))
ano = Column(Integer)
def __init__(self, str_marca="None", str_modelo="None", int_ano="None"):
"""
:param str_marca: (str).
:param str_modelo: (str).
:param int_ano: (int).
"""
self.marca = str_marca
self.modelo = str_modelo
self.ano = int_ano
def __repr__(self):
return "< Veículo {}, {}, {} >".format(self.marca, self.modelo, self.ano)
def insert(self):
session.add(self)
session.commit()
@classmethod
def find_by_column(self, session, str_column, var_search):
search = var_search
coluna = str_column
return session.query(self).filter_by(self.coluna=search).all()
@classmethod
def select_all(self, session):
return session.query(self)
My problem is in this method:
def find_by_column(self, session, str_column, var_search):
search = var_search
coluna = str_column
return session.query(self).filter_by(self.coluna=search).all()
Where I would like to pass the column name and the value to search everything in the function parameters.
What am I doing wrong?
Why don’t you use the filter_by? You can apply the column you want without having to rewrite the method in the template,
session.query(MyClasse).filter_by(meu_campo = 'valor')
– gato
@cat I believe you have not paid attention to my code. I am using filter_by. The point is that you did not want to pass the field name directly in filter_by, but rather by parameter in the function call.
– Cleber Nandi