Implement simple API filter interface for Sqlalchemy
Project description
Stack:
Installation
pip install pydantic-sqlalchemy-filter
Usage
Implement UserFilter
from pydantic_sqlalchemy_filter import BaseQueryBuilder, DatabaseModel
class UserFilter(BaseQueryBuilder):
username: str | None = Field(default=None)
order_by: str | None = Field(default='username', alias='orderBy')
def _filter(
self,
model: DatabaseModel,
exclude_deleted: bool = False,
query: sqlalchemy.Select | None = None,
) -> sqlalchemy.Select:
if query is None:
query = self.build_base_query(
model=model,
exclude_deleted=exclude_deleted,
)
if self.username is not None
query = query.filter_by(username=self.username)
return query
In FastAPI router:
@router.get(path='')
async def get_all_filtered_users(
query_builder: Annotated[UserFilter, Depends()],
session: Annotated[AsyncSession, Depends(get_async_session)],
) -> list[Users]:
query = query_builder.build_select_query(model=User)
results = await session.execute(query)
users = results.scalars().all()
return list(users)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pydantic_sqlalchemy_filter-0.0.7.tar.gz
.
File metadata
- Download URL: pydantic_sqlalchemy_filter-0.0.7.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.11.10 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d0cb752e1c53b5ae417cbf6bad168252b4ae93139fe1bd970c7a971f4b167ce |
|
MD5 | dfd62ffee8b51cf80667571747d51a30 |
|
BLAKE2b-256 | 367c3c7334c6747eb0df6c575c05416cd34ccd23c836936a533fad80800ceeaa |
File details
Details for the file pydantic_sqlalchemy_filter-0.0.7-py3-none-any.whl
.
File metadata
- Download URL: pydantic_sqlalchemy_filter-0.0.7-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.11.10 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ffb23bd88301c795b2cea64ec785eec92e9fc58a62c6460873252f5227e078f |
|
MD5 | 70bc0d08f9e693644a5a1f5cb27d379b |
|
BLAKE2b-256 | 6ced52a5a3dda5d068faa0f8f4aee3bb3eaa02fb4cf33e5eda0cb1de80670a7d |