Skip to main content

QueryParameterModel

QueryParameterModel is a Python library designed to simplify the parsing of query parameters in FastAPI applications. Utilizing Pydantic for validation, this package offers a streamlined approach to handling query parameters, including automatic conversion of camelCase query parameters from the URL to Python's snake_case convention.

Features

  • CamelCase to SnakeCase Conversion: Automatically convert camelCase query parameters from the URL to Python's snake_case convention.
  • Flexible Parsing: Parse query parameters into a full Pydantic model, which is particularly useful when handling multiple parameters in FastAPI routes.
  • List Support: Easily handle query parameters that are lists.
  • FastAPI Integration: Seamlessly integrate with FastAPI’s dependency injection system.

Installation

You can install QueryParameterModel via pip:

pip install fastapi-query-parameter-model

Usage

Define Your Model

Create a model by inheriting from QueryParameterModel and define your query parameters. CamelCase query parameters from the URL will be converted to snake_case in your model.

from fastapi_query_parameter_model import QueryParameterModel
from fastapi import Query

class SmallTestQueryParamsModels(QueryParameterModel):
    title: str
    age: int = 0
    is_major: bool = Query(alias="isMajor", default=False)
    array: list[str] = []

Integrate with FastAPI

Use FastAPI’s dependency injection to parse query parameters into the model:

from fastapi import FastAPI, Depends
from fastapi_query_parameter_model import QueryParameterModel

app = FastAPI()

@app.get("/small-test")
async def small_test(
    query_params: SmallTestQueryParamsModels = Depends(SmallTestQueryParamsModels.parser)
):
    return query_params.model_dump()

Example Usage

Here are two example endpoints demonstrating how to use the models with FastAPI:

from fastapi import FastAPI, Depends, Query
from fastapi_query_parameter_model import QueryParameterModel

app = FastAPI()

class SmallTestQueryParamsModels(QueryParameterModel):
    title: str
    age: int = 0
    is_major: bool = Query(alias="isMajor", default=False)
    array: list[str] = []

class FullTestQueryParamsModels(QueryParameterModel):
    title: str
    age: int = 0
    is_major: bool = Query(alias="isMajor", default=False)
    str_array: list[str] = Query(alias="strArray", default=[])
    int_array: list[int] = Query(alias="intArray", default=[])

@app.get("/small-test")
async def small_test(
    query_params: SmallTestQueryParamsModels = Depends(SmallTestQueryParamsModels.parser)
):
    return query_params.model_dump()

@app.get("/full-test")
async def full_test(
    query_params: FullTestQueryParamsModels = Depends(FullTestQueryParamsModels.parser)
):
    return query_params.model_dump()

Example Response

Using the following query : small-test?title=thisATitle&isMajor=true&array[]=a&array[]=b&array[]=c

For the /small-test route, a sample response might look like this:

{
  "title": "thisATitle",
  "age": 0,
  "is_major": true,
  "array": ["a", "b", "c"]
}

This response shows how the query parameters are parsed and returned as a Pydantic model instance with snake_case attributes.

Methods

  • parser(request: Request): Parses the query parameters from a FastAPI Request object and returns an instance of the model with converted snake_case attributes.
  • model_dump(): This is a native pydantic function, only used in this example, to show the result of the parsed query parameters model

Contributing

Contributions are welcome! To contribute:

  1. Fork the repository.
  2. Create a new branch for your feature or bug fix.
  3. Commit your changes.
  4. Push your branch and open a pull request.

Please ensure that your code adheres to the existing style and includes tests where applicable.

License

QueryParameterModel is licensed under the MIT License. See the LICENSE file for details.

Contact

For questions or feedback, feel free to open an issue on the GitHub repository.


This revised README should more accurately reflect the functionality of your QueryParameterModel and help users understand its features and usage. Let me know if there are any other adjustments or additional details you’d like to include!

Metadata

Release files for fastapi-query-parameter-model 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-query-parameter-model 0.1.2
File Size Uploaded
fastapi_query_parameter_model-0.1.2.tar.gz 3.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-query-parameter-model 0.1.2
File Interpreter ABI Platform
fastapi_query_parameter_model-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 8.9 kB

Release files / fastapi_query_parameter_model-0.1.2.tar.gz

Download URL fastapi_query_parameter_model-0.1.2.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
74adf4dcf747fdd867298f8d485d2f2362d31fb8f3aa83cf5055aa271d402a62
BLAKE2b-256 checksum
How to use checksums
ff7f809799b4408db2bb26dd5e83bb7fa4cef3eec0f17f84b1d77a888298361b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release files / fastapi_query_parameter_model-0.1.2-py3-none-any.whl

Download URL fastapi_query_parameter_model-0.1.2-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bdaeb404b2350595867810b9354e9db98d4182028f2c505fba8239bd47022b62
BLAKE2b-256 checksum
How to use checksums
797b8311bcd353230f985506227f71c6d85d3b16d380a7d756cbf930736d302b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page