https://github.com/sinisaos/wtforms-piccolo
Server-side form generation utilities for Piccolo ORM Table class
https://github.com/sinisaos/wtforms-piccolo
form-generator piccolo wtforms
Last synced: 4 months ago
JSON representation
Server-side form generation utilities for Piccolo ORM Table class
- Host: GitHub
- URL: https://github.com/sinisaos/wtforms-piccolo
- Owner: sinisaos
- License: mit
- Created: 2022-05-25T06:37:01.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-06-02T04:31:55.000Z (about 3 years ago)
- Last Synced: 2024-10-04T02:16:30.577Z (8 months ago)
- Topics: form-generator, piccolo, wtforms
- Language: Python
- Homepage: https://pypi.org/project/wtforms-piccolo/
- Size: 24.4 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Server-side form generation utilities for Piccolo ORM Table class.
Based on the code found in [wtforms.ext](https://wtforms.readthedocs.io/en/2.3.x/ext/) and provides a bridge between Piccolo ORM tables and WTForms.# Installation
```bash
pip install wtforms-piccolo
```
# UsageExample usage:
```python
# table.py
from piccolo.apps.user.tables import BaseUser
from piccolo.columns import Boolean, ForeignKey, Integer, Text, Varchar
from piccolo.table import Tableclass 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.
```shell
TaskForm = table_form(Task)
```Generate a form based on the table, excluding 'id' and 'views'.
```shell
TaskForm = table_form(Task, exclude=['id', 'views'])
```
Generate a form based on the table, only including 'name' and 'description'.```shell
TaskForm = table_form(Task, only=['name', 'description'])
```
The form can be generated setting field arguments:```python
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:```python
# 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:
```html
{% extends "base.html" %}
{% block content %}
Edit Task
{% for field in form %}
{{ field.label }}:
{{ field(class="form-control") }}
{% for error in field.errors %}
*{{ error }}
{% endfor %}
{% endfor %}
{% endblock %}
```The full example of the Starlette web application is in example folder.