wtforms integration for peewee
Project description
wtf-peewee
this project, based on the code found in wtforms.ext, provides a bridge
between peewee models and wtforms, mapping model fields to form fields.
example usage:
first, create a couple basic models and then use the model_form class factory to create a form for the Entry model:
from peewee import *
from wtfpeewee.orm import model_form
import wtforms
class PasswordField(TextField):
""" Custom-defined field example. """
def wtf_field(self, model, **kwargs):
return wtforms.PasswordField(**kwargs)
class Blog(Model):
name = CharField()
def __unicode__(self):
return self.name
class Entry(Model):
blog = ForeignKeyField(Blog)
title = CharField()
body = TextField()
protected = PasswordField()
def __unicode__(self):
return self.title
# create a form class for use with the Entry model
EntryForm = model_form(Entry)
Example implementation for an "edit" view using Flask:
@app.route('/entries/<int:entry_id>/', methods=['GET', 'POST'])
def edit_entry(entry_id):
try:
entry = Entry.get(id=entry_id)
except Entry.DoesNotExist:
abort(404)
if request.method == 'POST':
form = EntryForm(request.form, obj=entry)
if form.validate():
form.populate_obj(entry)
entry.save()
flash('Your entry has been saved')
else:
form = EntryForm(obj=entry)
return render_template('blog/entry_edit.html', form=form, entry=entry)
Example template for above view:
{% extends "layout.html" %}
{% block body %}
<h2>Edit {{ entry.title }}</h2>
<form method="post" action="">
{% for field in form %}
<p>{{ field.label }} {{ field }}</p>
{% endfor %}
<p><button type="submit">Submit</button></p>
</form>
{% 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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file wtf_peewee-3.1.0.tar.gz.
File metadata
- Download URL: wtf_peewee-3.1.0.tar.gz
- Upload date:
- Size: 65.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
636795565d138392b5230de513a89cc6ad0024a76a7c806ddb4c107523b5f41a
|
|
| MD5 |
47253eefb197891154051799b6b25754
|
|
| BLAKE2b-256 |
e5d6f362e700057f46d73c18f0d81bfedeed4cbf081031313db36c1b8bb346b6
|
File details
Details for the file wtf_peewee-3.1.0-py3-none-any.whl.
File metadata
- Download URL: wtf_peewee-3.1.0-py3-none-any.whl
- Upload date:
- Size: 14.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e05d274ca5a194fb7c11a5bfeb41bf1f7ff2861ba436cb7fbee966e8b824e82
|
|
| MD5 |
6501a61089d5d2bfefccfd9ee828d27d
|
|
| BLAKE2b-256 |
91c7c889749064a4bdd372d61c277df0b05c71e88c699c040855b8cd40c108ab
|