Skip to main content

Pydantic-based HTTP forms

Project description

Fodantic

Pydantic-based HTTP forms.

Pydantic is the most widely used data validation library for Python, but it's hard to use it with regular HTTP forms... until now.

Fodantic allow you to quickly wrap your Pydantic models and use them as forms: with support for multiple values, checkboxes, error handling, and integration with your favorite ORM.

A simple example

from fodantic import formable
from pydantic import BaseModel

@formable
class UserModel(BaseModel):
    name: str
    friends: list[int]
    active: bool = True

# This is just an example.
# You would use the request POST data of your web framework instead,
# for example `request_data = request.form` in Flask
from multidict import MultiDict
request_data = MultiDict([
  ("name", "John Doe"),
  ("friends", "2"),
  ("friends", "3"),
])


# The magic
form = UserModel.as_form(request_data, object=None)

print(form)
#> UserModel.as_form(name='John Doe', friends=[2, 3], active=False)
print(form.fields["name"].value)
#> John Doe
print(form.fields["name"].error)
#> None
print(form.save())  # Can also update the `object` passed as an argument
#> {'name': 'John Doe', 'friends': [2, 3], 'active': False}

Installation

pip install fodantic

Requirements

  • Python 3.10+
  • Pydantic 2.*

Documentation

List fields

Fields defined as of type list, tuple, or a derivated type, will be marked as expecting multiple values. A <select multiple> and a group of checkboxes charing the same name (but different values) are the most common examples of how these fields look on a form. Fodantic will use the getall(*) method on the request data to get a list of all the values under the same name.

(*) Also called getlist in many web frameworks.

Booleans fields

Boolean fields are treated special because of how browsers handle checkboxes:

  • If not checked: the browser doesn't send the field at all, so the missing field will be interpreted as False.
  • If checked: It sends the "value" attribute, but this is optional, so it could send an empty string instead. So any value other than None will be interpreted as True.

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

fodantic-0.0.5.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

fodantic-0.0.5-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fodantic-0.0.5.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.11.9

File hashes

Hashes for fodantic-0.0.5.tar.gz
Algorithm Hash digest
SHA256 a38ac32be86425b03e0d0aa5d3052657fd43a3be8ff87a2de3f0b970e83f0dcc
MD5 ea9542c2c6df2af97f45529f8faae3be
BLAKE2b-256 ce7c546c56f8ed9f53e5f50ebf64135becc33e0d39b7e7470b6d56ba6d12d78d

See more details on using hashes here.

File details

Details for the file fodantic-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: fodantic-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.11.9

File hashes

Hashes for fodantic-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 113f650d66cab7b0eebe02b4aa4b9c489bb3dc16d971090ac25647e3a67fe604
MD5 f983ba6ae52b0fb4f131f30009713bdf
BLAKE2b-256 e72c7d87a6fd5126cc48e067a4a535585a81429d7b72771b3110f5c3bcf34f87

See more details on using hashes here.

Supported by

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