Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing db.session from Django app #104

Closed
alik472 opened this issue Sep 11, 2020 · 1 comment
Closed

Accessing db.session from Django app #104

alik472 opened this issue Sep 11, 2020 · 1 comment

Comments

@alik472
Copy link

alik472 commented Sep 11, 2020

Hi, I am trying to query a subquery in Django, and in my Flask app I used to query like this:

db.session.query(some_subquery)

How can I access session instance or alternatively how can I do the same query from Model?

I have tried this:

some_subquery.sa.query()
and
some_subquery.query()

Both throwing an exception of attribute/method does not exist.

Please help.

@alik472
Copy link
Author

alik472 commented Sep 11, 2020

After some googling I found this link.
https://rodic.fr/blog/sqlalchemy-django/

I have implemented the session instance this way, but I am not sure it is the best way:

from sqlalchemy.orm import sessionmaker
from aldjemy.core import get_engine

def Session():
    engine = get_engine()
    _Session = sessionmaker(bind=engine)
    return _Session()

session = Session()

session.query(some_subquery)

@alik472 alik472 closed this as completed Sep 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant