# Generic Model
Este módulo permite manejar distintos métodos de SQLAlchemy (opens new window) para la gestión de Base de Datos.
Consulta el apartado de configuración de MySQL para el correcto uso de este módulo.
Al usar SQLAlchemy (opens new window) como ORM, se brinda un acceso a bases de datos completo, eficiente y de alto rendimiento, adaptados al lenguaje de programación Python.
# Funciones disponibles
La clase GenericModel, implementada en el BackEndBase contiene las siguientes funciones:
__tablename__
(declared_attr (opens new window))save
multi_save
update
delete
delete_table
get_schema
get_all
get_by_id
get_by
where_
select_from_view
dict_to_obj
to_json
raw_json
raw_query
# tablename (declared_attr (opens new window))
@declared_attr
def __tablename__(cls):
...
# save
@classmethod
def save(cls, _data=None):
...
# multi_save
@classmethod
def multi_save(cls, _data=None):
...
# update
@classmethod
def update(cls, _data=None):
...
# delete
@classmethod
def delete(cls, _data=None):
...
# delete_table
@classmethod
def delete_table(cls, confirm, re_confirm):
...
# get_schema
@classmethod
def get_schema(cls):
...
# get_all
@classmethod
def get_all(cls, _limit=0, select_fields=None):
...
# get_by_id
@classmethod
def get_by_id(cls, _id=0):
...
# get_by
@classmethod
def get_by(cls, _name=None, _value=None, result_fetch='one'):
...
# where_
@classmethod
def where_(cls, select_fields, _conds, result_fetch='one'):
...
# select_from_view
@classmethod
def select_from_view(cls, _view=None, select_fields=None, _conds=None, result_fetch='one'):
...
# dict_to_obj
@classmethod
def dict_to_obj(cls, _dict=None, _name='from_dict'):
...
# to_json
@classmethod
def to_json(cls, obj=None):
...
# raw_json
@classmethod
def raw_json(cls, obj=None, default='dict'):
...
# raw_query
@classmethod
def raw_query(cls, _query):
...
# Un ejemplo
from app.companies.models.Companies import Company
...
try:
_new_company = Company()
_new_company.name = name
_new_company.address = address
_new_company.email = email
result = Companies.save(_new_company)
if result is None:
result_app = Company.to_json(_new_company)
print('new Companies created!', result_app)
else:
print(str(result))
except Exception as e:
print("Save Companies Exception:", e)
El resultado del ejemplo anterior, en caso de que todo esté bien:
>>> new Companies created!
Los resultados varían en caso de que ocurra algún error.
{
"data": null,
"message": "missing fields: ['name', 'address', 'email']",
"status": 400
}
etc...
# Más información
Para mas información consulte:
- Documentación oficial de SQLAlchemy (opens new window)
- Dialectos utilizados en SQLAlchemy (opens new window)
- Engine Configuration (opens new window)
- Using the Session (opens new window)
- Performance (opens new window)
- PyMySQL (opens new window), biblioteca cliente de MySQL, utilizada el BackEndBase
- Python Database API Specification (opens new window)
- La sección de issues (opens new window) del BackEndBase