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

Uploaded Source

File details

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

File metadata

  • Download URL: sanic_filter-0.0.7.tar.gz
  • Upload date:
  • Size: 3.7 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.7.tar.gz
Algorithm Hash digest
SHA256 0d72eb0781914066cd86fd295240d01d96aedcffbe5ae4c433554de0a720e401
MD5 7c7aac091a1451642ddeedcb21f37c13
BLAKE2b-256 c22ca05a356ae19094784ececfc3805a6ac4640ef2525fb8a9ead26220941812

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