Skip to main content

Form generation utilities for Piccolo ORM Table class

Project description

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 piccolo-wtforms

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 %}

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

wtforms_piccolo-0.1.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wtforms_piccolo-0.1.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file wtforms_piccolo-0.1.0.tar.gz.

File metadata

  • Download URL: wtforms_piccolo-0.1.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.6

File hashes

Hashes for wtforms_piccolo-0.1.0.tar.gz
Algorithm Hash digest
SHA256 55b96dea27a930943ea58aca0371c7d93fd367977f45915615e728ff81858cc4
MD5 9fe06fc6027b45564d428e7547338da7
BLAKE2b-256 7eb8273d3b139b4d6196a5d61ceddf4b8a8017e6cf3becfeae80d8ca2279a266

See more details on using hashes here.

File details

Details for the file wtforms_piccolo-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for wtforms_piccolo-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dfccb791a8381e3f16431e84cd7aa59878dbf1797948f3760a0dc29dd2fe18f1
MD5 0001f739f0b8ce7c9f0069c2b624e505
BLAKE2b-256 c3914d2a64316064d6020b90c63b973659a5b717ffcbb7583b983f3df5bbe142

See more details on using hashes here.

Supported by

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