Coder Social home page Coder Social logo

sqlalchemy-manager's Introduction

Managers for SQLAlchemy.

Manager for model, methods added in runtime to query.

Installation

$ [sudo] pip install sqlalchemy-manager

Documentation

alchmanager.ManagedQuery

Manager for Query.

Example

from sqlalchemy.orm import sessionmaker

from alchmanager import ManagedQuery, ManagedSession


engine = create_engine('sqlite:///:memory:')
session = sessionmaker(query_cls=ManagedQuery,
                       bind=engine)()
Base = declarative_base()

class MainManager:

    @staticmethod
    def is_index(query):
        return query.filter_by(is_index=True)

    @staticmethod
    def is_public(query):
        return query.filter_by(is_public=True)

class Test(Base):
    id = Column(Integer, primary_key=True)
    is_public = Column(Boolean, default=False)
    is_index = Column(Boolean)

    __manager__ = MainManager

session.query(Test).is_index().filter_by(id=1).is_public()

alchmanager.ManagedSession

Manager for Session. Decorator load_manager() for register methods into session.

Example

from sqlalchemy.orm import sessionmaker
from alchmanager import ManagedQuery, ManagedSession

engine = create_engine('sqlite:///:memory:')
session = sessionmaker(class_=ManagedSession,
                       bind=engine)()


@session.load_manager()
class MainSessionManager:

    @staticmethod
    def published(query):
        return query.filter_by(is_public=True)

    @staticmethod
    def has_index(query):
        return query.filter_by(is_index=True)

session.query(TestModel).has_index().published().count()

sqlalchemy-manager's People

Contributors

d1ffuz0r avatar pablodiguerero avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

sqlalchemy-manager's Issues

Проблемы с Query объектом в запросе

Класс менеджера

class RealtyManager(DataTableManager):

    @staticmethod
    def with_author(query):
        from models.realty.realty import Realty

        q = query.filter(Realty.author_id == 1)
        return q

    @staticmethod
    def with_id(query):
        q = query.filter_by(id=1)
        return q

ORM модель

class Realty(db.Model, LoggableMixin):
    # ...
    __manager__ = RealtyManager

Исполняемый код

q = Realty.query.filter(sa.and_(*search_filter))
print(q._criterion)
q1 = q.filter(Realty.id == 1)
print(q1._criterion)
q2 = q1.with_id()
print(q2._criterion)
q3 = q2.with_author()
print(q3._criterion)

Вывод в консоли

crm_realty_flat.market_type_id = :market_type_id_1
crm_realty_flat.market_type_id = :market_type_id_1 AND crm_realty.id = :id_1
crm_realty.id = :id_1
crm_realty.author_id = :author_id_1

Они же должны сохраняться?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.