Skip to main content

Query filter for Sanic

Project description

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.5.tar.gz (3.8 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: sanic_filter-0.0.5.tar.gz
  • Upload date:
  • Size: 3.8 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.5.tar.gz
Algorithm Hash digest
SHA256 560c4f76da09c1baabd09cff3387bd156e60e72864c1c84dd322ce92c1163fe0
MD5 3b46c930a2b41e1e199392243364486d
BLAKE2b-256 0c8cb90e6d4a276b0af6127dc8152ef577e2e347b5a5705e04aec99550b56a5d

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