Skip to main content

WTF Tortoise

Form generation utilities for Tortoise ORM Model class. Based on the code found in wtforms.ext and provides a bridge between Tortoise ORM models and wtforms.

Packaged from Sinisaos, who did all the work. Please support his efforts.

For testing/direct usage, just download repo, unzip and put in root of your project. Otherwise, just run: pip install wtf-toroise.

Now you can import wtftortoise.orm like in edit example below.

Example usage:

from tortoise.models import Model
from tortoise import fields


class Book(Model):
    id = fields.IntField(pk=True)
    title = fields.CharField(max_length=255)
    content = fields.TextField()
    created = fields.DatetimeField(auto_now_add=True)

    def __str__(self):
        return self.title

    class Meta:
        ordering = ["-id"]

Generate a form based on the model.

BookForm = model_form(Book)

Properties from the model can be excluded from the generated form, or it can include just a set of properties. For example:

Generate a form based on the model, excluding 'id' and 'created'.

BookForm = model_form(Book, exclude=['id', 'created'])

Generate a form based on the model, only including 'title' and 'content'.

BookForm = model_form(Book, only=['title', 'content'])

The form can be generated setting field arguments:

BookForm = model_form(Book, only=['title', 'content'], field_args={
    'title': {
        'label': 'Your new label',
    },
    'content': {
        'label': 'Your new label',
    }
})

Example implementation for an edit view using Starlette:

# other imports
from wtftortoise.orm import model_form

@app.route("/{id:int}/edit", methods=["GET", "POST"])
async def edit(request):
    id = request.path_params.get("id", None)
    book = await Book.get(id=id)
    data = await request.form()
    BookForm = model_form(Book, exclude=["id", "created"])
    form = BookForm(obj=book, formdata=data)
    if request.method == "POST" and form.validate():
        form.populate_obj(book)
        await book.save()
        return RedirectResponse(url="/", status_code=302)

    return templates.TemplateResponse(
        "edit.html", {
            "request": request,
            "form": form
        }
    )

Example template for above view using Jinja and Bootstrap:

{% extends "base.html" %}
{% block content %}
<main role="main">
    <br><br>
    <div class="container">
        <h2>Edit Book</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 %}

Metadata

Release files for wtf-tortoise 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for wtf-tortoise 0.0.4
File Size Uploaded Provenance Actions
wtf-tortoise-0.0.4.tar.gz 5.4 kB Without provenance Details for wtf-tortoise-0.0.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for wtf-tortoise 0.0.4
File Interpreter ABI Platform Actions
wtf_tortoise-0.0.4-py3-none-any.whl Python 3 none any Details for wtf_tortoise-0.0.4-py3-none-any.whl

Total release size: 11.2 kB

Release files / wtf-tortoise-0.0.4.tar.gz

Download URL wtf-tortoise-0.0.4.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
2367f280d50dc5197ad0c253f3309665b3f946a865ea300794bcaf8f6c9c8f96
BLAKE2b-256 checksum
How to use checksums
722717bde46382f0b10d02b41cb83526f4ae09e782e348f45a56694bbdaf7743
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.4

Release files / wtf_tortoise-0.0.4-py3-none-any.whl

Download URL wtf_tortoise-0.0.4-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
29ce563ba10622d02411def5d54f795728cf1e91d3a1eb310492fe823b8c609d
BLAKE2b-256 checksum
How to use checksums
849a44b25ab03c41689f64ddfb66a4c4d463fa4e3055bede54f38f137d606408
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.4

Security for wtf-tortoise 0.0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page