Implementation of WTForms for Tortoise ORM
Project description
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 %}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file wtf-tortoise-0.0.4.tar.gz
.
File metadata
- Download URL: wtf-tortoise-0.0.4.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2367f280d50dc5197ad0c253f3309665b3f946a865ea300794bcaf8f6c9c8f96 |
|
MD5 | debfc24af7166baf489c451fa1ed8be8 |
|
BLAKE2b-256 | 722717bde46382f0b10d02b41cb83526f4ae09e782e348f45a56694bbdaf7743 |
File details
Details for the file wtf_tortoise-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: wtf_tortoise-0.0.4-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 29ce563ba10622d02411def5d54f795728cf1e91d3a1eb310492fe823b8c609d |
|
MD5 | b5738e322e047775f5d05864b2f67a1c |
|
BLAKE2b-256 | 849a44b25ab03c41689f64ddfb66a4c4d463fa4e3055bede54f38f137d606408 |