Skip to main content

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

sanic_filter-0.0.3.tar.gz (3.9 kB view details)

Uploaded Source

File details

Details for the file sanic_filter-0.0.3.tar.gz.

File metadata

  • Download URL: sanic_filter-0.0.3.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

Hashes for sanic_filter-0.0.3.tar.gz
Algorithm Hash digest
SHA256 e92ba8533a4b6ffaca11d690203bc97c70ea58e15954f624a916b4e9abd91721
MD5 ec65ecf948055684a92f73a0d53b507c
BLAKE2b-256 d0796f54407ade20c1342ebe13befbd4e05ae2519bd45016f0431b055099ca4d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page