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

Remove unnecessary usages of scoped_session from docs #641

Merged
merged 2 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Other changes:
Thanks :user:`GabrielC101` for the suggestion.
* Docs: Document methods of `SQLAlchemySchema <marshmallow_sqalalchemy.SQLAlchemySchema>`
and `SQLAlchemyAutoSchema <marshmallow_sqalchemy.SQLAlchemyAutoSchema>` (:issue:`619`).
* Docs: Various documentation improvements (:pr:`635`, :pr:`636`, :pr:`639`).
* Docs: Various documentation improvements (:pr:`635`, :pr:`636`, :pr:`639`, :pr:`641`).

1.2.0 (2025-01-09)
++++++++++++++++++
Expand Down
18 changes: 10 additions & 8 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ Declare your models
DeclarativeBase,
backref,
relationship,
scoped_session,
sessionmaker,
)

from marshmallow_sqlalchemy import SQLAlchemySchema, auto_field

engine = sa.create_engine("sqlite:///:memory:")
session = scoped_session(sessionmaker(bind=engine))
Session = sessionmaker(engine)


class Base(DeclarativeBase):
Expand Down Expand Up @@ -112,17 +111,20 @@ Make sure to declare `Models` before instantiating `Schemas`. Otherwise `sqlalch
author = Author(name="Chuck Paluhniuk")
author_schema = AuthorSchema()
book = Book(title="Fight Club", author=author)
session.add(author)
session.add(book)
session.commit()

with Session() as session:
session.add(author)
session.add(book)
session.commit()

dump_data = author_schema.dump(author)
print(dump_data)
# {'id': 1, 'name': 'Chuck Paluhniuk', 'books': [1]}

load_data = author_schema.load(dump_data, session=session)
print(load_data)
# <Author(name='Chuck Paluhniuk')>
with Session() as session:
load_data = author_schema.load(dump_data, session=session)
print(load_data)
# <Author(name='Chuck Paluhniuk')>

Get it now
==========
Expand Down
45 changes: 24 additions & 21 deletions docs/recipes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,26 @@ Serialization will look like this:

from pprint import pprint

from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.orm import sessionmaker

engine = sa.create_engine("sqlite:///:memory:")
session = scoped_session(sessionmaker(bind=engine))
Session = sessionmaker(engine)

Base.metadata.create_all(engine)

user = User(full_name="Freddie Mercury")
post = BlogPost(title="Bohemian Rhapsody Revisited", author=user)
session.add_all([user, post])
session.commit()
with Session() as session:
user = User(full_name="Freddie Mercury")
post = BlogPost(title="Bohemian Rhapsody Revisited", author=user)

session.add_all([user, post])
session.commit()

blog_post_schema = BlogPostSchema()
data = blog_post_schema.dump(post)
pprint(data, indent=2)
# { 'author': {'full_name': 'Freddie Mercury', 'id': 1},
# 'id': 1,
# 'title': 'Bohemian Rhapsody Revisited'}
blog_post_schema = BlogPostSchema()
data = blog_post_schema.dump(post)
pprint(data, indent=2)
# { 'author': {'full_name': 'Freddie Mercury', 'id': 1},
# 'id': 1,
# 'title': 'Bohemian Rhapsody Revisited'}

Introspecting generated fields
==============================
Expand Down Expand Up @@ -269,14 +271,14 @@ Usage:
.. code-block:: python

import sqlalchemy as sa
from sqlalchemy.orm import declarative_base, scoped_session, sessionmaker
from sqlalchemy.orm import declarative_base, sessionmaker
from sqlalchemy import event
from sqlalchemy.orm import mapper

# Either import or declare setup_schema here

engine = sa.create_engine("sqlite:///:memory:")
session = scoped_session(sessionmaker(bind=engine))
Session = sessionmaker(engine)
Base = declarative_base()


Expand All @@ -295,15 +297,16 @@ Usage:

Base.metadata.create_all(engine)

author = Author(name="Chuck Paluhniuk")
session.add(author)
session.commit()
with Session() as session:
author = Author(name="Chuck Paluhniuk")
session.add(author)
session.commit()

# Model.__marshmallow__ returns the Class not an instance of the schema
# so remember to instantiate it
author_schema = Author.__marshmallow__()
# Model.__marshmallow__ returns the Class not an instance of the schema
# so remember to instantiate it
author_schema = Author.__marshmallow__()

print(author_schema.dump(author))
print(author_schema.dump(author))

This is inspired by functionality from `ColanderAlchemy <https://colanderalchemy.readthedocs.io/en/latest/>`_.

Expand Down
Loading