🪛 A simple pydantic to Form FastAPI model converter.

Overview

pyfa-converter

Makes it pretty easy to create a model based on Field [pydantic] and use the model for www-form-data.

How to install?

pip install pyfa_converter

How to simplify your life?

image


What do I need to do with the model?

  • We put the decorator @PydanticConverter.body for the model and enjoy.
  • data: YourPydanticModel = FormBody(YourPydanticModel)

If you want to accept a file on an endpoint, then the content-type for that endpoint changes from application/json to www-form-data.

FastAPI does not know how to override the pydantic schema so that parameters are passed as form. Even if you do

foo: CustomPydanticModel = Depends() all model attributes will be passed as query, but we want them to become body, that's what this library exists for.

Usually you use something along the lines of:

image

But, if we accept a lot of fields, then the function becomes very large (the number of attributes for the endpoint increases and it does not look very good).

Thanks to this library, it is possible to force the conversion of Field fields into fields of FastAPI Form with full preservation of all attributes (alias, gt, te, description, title, example and more...)

You might also like...
A simple weather tool. I made this as a way for me to learn Python, API, and PyPi packaging.
A simple weather tool. I made this as a way for me to learn Python, API, and PyPi packaging.

A simple weather tool. I made this as a way for me to learn Python, API, and PyPi packaging.

pwy - A simple weather tool.
pwy - A simple weather tool.

A simple weather tool. I made this as a way for me to learn Python, API, and PyPi packaging. Name changed from wwy to pwy.

A simple python script to execute a command when a YubiKey is disconnected

YubiKeyExecute A python script to execute a command when a YubiKey / YubiKeys are disconnected. ‏‏‎ ‎ How to use: 1. Download the latest release and d

A super simple wallet application for the NANO cryptocurrency that runs in the terminal
A super simple wallet application for the NANO cryptocurrency that runs in the terminal

Nano Terminal Wallet A super simple wallet application for the NANO cryptocurrency that runs in the terminal Written in 2021 by NinjaSnail1080 (Discor

This a simple tool to query the awesome ippsec.rocks website from your terminal
This a simple tool to query the awesome ippsec.rocks website from your terminal

ippsec-cli This a simple tool to query the awesome ippsec.rocks website from your terminal Installation and usage cd /opt git clone https://github.com

Simple Tool To Grab Like-Card Coupon
Simple Tool To Grab Like-Card Coupon

Simple Tool To Grab Like-Card Coupon

(BionicLambda Universal SHell) A simple shell made in Python. Docs and possible C port incoming.

blush 😳 (BionicLambda Universal SHell) A simple shell made in Python. Docs and possible C port incoming. Note: The Linux executables were made on Ubu

A simple reverse shell in python

RevShell A simple reverse shell in python Getting started First, start the server python server.py Finally, start the client (victim) python client.py

Un module simple pour demander l'accord de l'utilisateur dans une CLI.
Un module simple pour demander l'accord de l'utilisateur dans une CLI.

Demande de confirmation utilisateur pour CLI Présentation ask_lib est un module pour le langage Python proposant une seule fonction; ask(). Le but pri

Comments
  • Error: TypeError: unsupported operand type(s) for |: 'ModelMetaclass' and 'type'

    Error: TypeError: unsupported operand type(s) for |: 'ModelMetaclass' and 'type'

    Using the

    from pyfa_converter import FormDepends
    

    and get the error

    TypeError: unsupported operand type(s) for |: 'ModelMetaclass' and 'type'
    

    python: 3.9

    opened by Terryhung 4
  • Add License

    Add License

    Hi! The code in this repo seems super useful for us working with pydantic + fastapi, but we can't use it without a LICENSE. Could you add LICENSE file to the repo? MIT is a permissive one if you don't have an opinion otherwise

    opened by dzcode 3
  • Big update 1.0.0.0!

    Big update 1.0.0.0!

    • The decorator above the model is no longer required and will be removed in the next version!
    • New syntax:
    data: MyCustomModel = PyFaDepends(MyCustomModel, _type=Header)
    data: MyCustomModel = PyFaDepends(MyCustomModel, _type=Form)
    
    data: MyCustomModel = FormDepends(MyCustomModel)
    data: MyCustomModel = QueryDepends(MyCustomModel)
    
    • Added support for parameters specific only to FastAPI types in Pydantic Field. Example - Field(None, convert_underscores=True)
    opened by dotX12 0
  • error when adding Field constraint

    error when adding Field constraint

    On:

    python: 3.10 and 3.11 pyfa-converter: 1.0.3.0 (and master, which shows as 1.0.1.0) pydantic: 1.10.2

    If you add a Field constraint, pydantic raises a ValueError:

    For example, for the class in this repo, if you change it to (adding a gt constraint of 10:

    class PostContractSmallDoubleBodySchema(BaseModel):
        id: Optional[int] = Field(None, description="gwa", gt=10)
        title: Optional[str] = Field(None)
        data: Optional[List[int]]
    

    pydantic raises the following exception

    ImportError while loading conftest '/home/ben/repos/pyfa-converter/tests/conftest.py'.
    tests/conftest.py:6: in <module>
        from examples.main import app
    examples/main.py:47: in <module>
        @app.post("/test")
    .venv/lib/python3.11/site-packages/fastapi/routing.py:630: in decorator
        self.add_api_route(
    .venv/lib/python3.11/site-packages/fastapi/routing.py:569: in add_api_route
        route = route_class(
    .venv/lib/python3.11/site-packages/fastapi/routing.py:438: in __init__
        self.dependant = get_dependant(path=self.path_format, call=self.endpoint)
    .venv/lib/python3.11/site-packages/fastapi/dependencies/utils.py:292: in get_dependant
        sub_dependant = get_param_sub_dependant(
    .venv/lib/python3.11/site-packages/fastapi/dependencies/utils.py:122: in get_param_sub_dependant
        return get_sub_dependant(
    .venv/lib/python3.11/site-packages/fastapi/dependencies/utils.py:158: in get_sub_dependant
        sub_dependant = get_dependant(
    .venv/lib/python3.11/site-packages/fastapi/dependencies/utils.py:299: in get_dependant
        param_field = get_param_field(
    .venv/lib/python3.11/site-packages/fastapi/dependencies/utils.py:388: in get_param_field
        annotation = get_annotation_from_field_info(annotation, field_info, param_name)
    pydantic/schema.py:1011: in pydantic.schema.get_annotation_from_field_info
        ???
    E   ValueError: On field "id" the following field constraints are set but not enforced: gt.
    E   For more details see https://pydantic-docs.helpmanual.io/usage/schema/#unenforced-field-constraints
    

    I spent some time investigating and I'm still not sure why this is happening

    bug fixed 
    opened by falkben 9
Releases(1.0.4.0a)
Owner
null
A dec-bin converter uses 2's complement.

2's Complement Dec-Bin Converter A dec-bin converter uses 2's complement. Visit my Medium Post. What is 2's complement? Two's complement is the most c

C.H Jacky 9 Mar 1, 2022
A Python command-line utility for validating that the outputs of a given Declarative Form Azure Portal UI JSON template map to the input parameters of a given ARM Deployment Template JSON template

A Python command-line utility for validating that the outputs of a given Declarative Form Azure Portal UI JSON template map to the input parameters of a given ARM Deployment Template JSON template

Glenn Musa 1 Feb 3, 2022
Pynavt is a cli tool to create clean architecture app for you including Fastapi, bcrypt and jwt.

Pynavt _____ _ | __ \ | | | |__) | _ _ __ __ ___ _| |_ | ___/ | | | '_ \ / _` \ \ / /

Alejandro Castillo 1 Dec 13, 2021
Python Command Line Application (CLI) using Typer, SQLModel, Async-PostgrSQL, and FastAPI

pyflycli is a command-line interface application built with Typer that allows you to view flights above your location.

Kevin Zehnder 14 Oct 1, 2022
Customisable pharmacokinetic model accessible via bash CLI allowing for variable dose calculations as well as intravenous and subcutaneous administration calculations

Pharmacokinetic Modelling Group Project A PharmacoKinetic (PK) modelling function for analysis of injected solute dynamics over time, developed by Gro

null 1 Oct 24, 2021
Dead simple CLI tool to try Python packages - It's never been easier! :package:

try - It's never been easier to try Python packages try is an easy-to-use cli tool to try out Python packages. Features Install specific package versi

Timo Furrer 659 Dec 28, 2022
A simple discord slash command handler for for discord.py.

A simple discord slash command handler for discord.py About ⦿ Installation ⦿ Disclaimer ⦿ Examples ⦿ Documentation ⦿ Discussions Note that master bran

null 641 Jan 3, 2023
Simple command line tool for text to image generation using OpenAI's CLIP and Siren (Implicit neural representation network)

Simple command line tool for text to image generation using OpenAI's CLIP and Siren (Implicit neural representation network)

Phil Wang 4.4k Jan 9, 2023
QueraToCSV is a simple python CLI project to convert the Quera results file into CSV files.

Quera is an Iranian Learning management system (LMS) that has an online judge for programming languages. Some Iranian universities use it to automate the evaluation of programming assignments.

Amirmahdi Namjoo 16 Nov 11, 2022
Simple CLI tool to track your cryptocurrency portfolio in real time.

Simple tool to track your crypto portfolio in realtime. It can be used to track any coin on the BNB network, even obscure coins that are not listed or trackable by major portfolio tracking applications.

Trevor White 69 Oct 24, 2022