Server-side form generation utilities for Piccolo ORM Table class. Based on the code found in wtforms.ext and provides a bridge between Piccolo ORM tables and WTForms.
Installation
pip install wtforms-piccolo
Usage
Example usage:
# table.py
from piccolo.apps.user.tables import BaseUser
from piccolo.columns import Boolean, ForeignKey, Integer, Text, Varchar
from piccolo.table import Table
class Task(Table):
"""
An example table.
"""
name = Varchar(required=True, null=False)
description = Text(required=True, null=False)
views = Integer(required=True, null=False, default=0)
completed = Boolean(default=False)
task_user = ForeignKey(references=BaseUser)
Generate a form based on the table.
TaskForm = table_form(Task)
Generate a form based on the table, excluding 'id' and 'views'.
TaskForm = table_form(Task, exclude=['id', 'views'])
Generate a form based on the table, only including 'name' and 'description'.
TaskForm = table_form(Task, only=['name', 'description'])
The form can be generated setting field arguments:
TaskForm = table_form(Task, only=['name', 'description'], field_args={
'name': {
'label': 'Your new label',
},
'description': {
'label': 'Your new label',
}
})
Example implementation for an edit view using Starlette web app:
# app.py
# other imports
from wtforms_piccolo.orm import table_form
@app.route("/{id:int}/", methods=["GET", "POST"])
async def edit(request):
path_id = request.path_params["id"]
item = await Task.objects().get(Task.id == path_id).run()
users = await BaseUser.select().run()
data = await request.form()
TaskForm = table_form(Task, exclude=["id"])
form = TaskForm(obj=item, formdata=data)
# FK select field
form.task_user.choices = [(i["id"], i["username"]) for i in users]
if request.method == "POST" and form.validate():
form.populate_obj(item)
await item.save().run()
return RedirectResponse(url="/", status_code=302)
return templates.TemplateResponse(
"edit.html",
{
"request": request,
"form": form,
"table_name": Task._meta.tablename,
},
)
Example template for above view using Jinja and Bootstrap:
{% extends "base.html" %}
{% block content %}
<main role="main">
<br><br>
<div class="container">
<h2>Edit Task</h2>
<br>
<form method="POST">
{% for field in form %}
<div class="form-group">
{{ field.label }}:
{{ field(class="form-control") }}
{% for error in field.errors %}
<span style="color: red;">*{{ error }}</span>
{% endfor %}
</div>
{% endfor %}
<p><input class="btn btn-primary" type="submit" value="Submit"></p>
</form>
</div> <!-- /container -->
<hr>
</main>
{% endblock %}
The full example of the Starlette web application is in example folder.
Metadata
Release files for wtforms-piccolo 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| wtforms_piccolo-0.2.1.tar.gz | 5.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| wtforms_piccolo-0.2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.3 kB
Release files / wtforms_piccolo-0.2.1.tar.gz
| Download URL | wtforms_piccolo-0.2.1.tar.gz |
|---|---|
| Size | 5.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0cd7e6e88860a4dbfff9d7f70170867f435238452a3723ef5d192394749c6152
|
|
BLAKE2b-256 checksum How to use checksums |
e34e724f736969d7714bfd434be72a9f9c6e93471c09bce6433bd21c307adef6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.12
|
Release files / wtforms_piccolo-0.2.1-py3-none-any.whl
| Download URL | wtforms_piccolo-0.2.1-py3-none-any.whl |
|---|---|
| Size | 5.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b52ab4331117e5b2e8e4befb60d3c6da6d57d5cc213b3cd14dff0cb8a9d7879f
|
|
BLAKE2b-256 checksum How to use checksums |
5e8a87fa629ba34716de24ddeb64c4f14c6ed3a7a0be3193aba75b43da6adf6a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.12
|