Query filter for Sanic
Project description
Github
https://github.com/naginnn/sanic-filter/tree/develop
Sanic filter
Required:
- Python: >=3.9
- SQLAlchemy: >=2.0.20
Optional
- Sanic: >=23.3.0
- Dataclasses
Installation
pip install sanic-filter
Description
You define the fields you want to be able to filter on as well as the type of operator
name: str = Query(option='ilike')
You can make some field mandatory
class MyFilter(Filter)
name: str = Query(option='ilike', required=True)
Query
?name=moon,sun
sqlAlchemy
SELECT * FROM table WHERE table.name LIKE :name_1 OR table.name LIKE :name_2"
Supported operators:
- like
- ilike
- in
- ==
- !=
If you want to search by JSON fields
#SqlAlchemy model
properties = Column(JSON, nullable=True)
Filter supports one level of nesting
color: str = Query(option='ilike’, json_column='properties', required=True)
Query
?name=moon,sun
sqlAlchemy
SELECT * FROM table WHERE table.name LIKE :name_1 OR table.name LIKE :name_2"
Returned fields
You can specify the returned fields of the models Filter
fields: str = Query(option='returned_fields')
It will look like this SqlAlchemy model
class Car(BaseModel):
__tablename__ = "cars"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
name = Column(String, nullable=True)
order = Column(String, nullable=True)
properties = Column(JSON, nullable=True)
Query
?name=moon,sun
sqlAlchemy
SELECT * FROM table WHERE table.name LIKE :name_1 OR table.name LIKE :name_2"
The search for fields is performed automatically, if the field has the same name, use model name
sqlalchemy models
class Car(BaseModel):
__tablename__ = "cars"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
class Garage(BaseModel):
__tablename__ = «garages»
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
filter
id: str = Query(option='ilike', required=True, model=Car)
routers
query.filter(select(Car, Garage))
If you want to return fields with the same name, but from different models, use the alias parameter id from Car
car_id: str = Query(option='ilike’, alias=‘id’, model=Car)
garage_id: str = Query(option='ilike', alias=‘id’, model=Garage)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file sanic_filter-0.0.4.tar.gz.
File metadata
- Download URL: sanic_filter-0.0.4.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e4150eaeafed0550dd00130422d00e1f739fe47fe3ee0e414dd78c80870ce436
|
|
| MD5 |
da586594e92b63aa1ec5c4d7c78e2483
|
|
| BLAKE2b-256 |
c4139284a9fb7c23f16ead6daddabcaf9525d7a9a30385006b5731232188bb54
|