WTForms integration for peewee models
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
class Blog(Model):
name = CharField()
def __unicode__(self):
return self.name
class Entry(Model):
blog = ForeignKeyField(Blog)
title = CharField()
body = TextField()
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 %}
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
class Blog(Model):
name = CharField()
def __unicode__(self):
return self.name
class Entry(Model):
blog = ForeignKeyField(Blog)
title = CharField()
body = TextField()
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
wtf-peewee-0.2.0.tar.gz
(61.3 kB
view details)
File details
Details for the file wtf-peewee-0.2.0.tar.gz.
File metadata
- Download URL: wtf-peewee-0.2.0.tar.gz
- Upload date:
- Size: 61.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
95769478e43a5bf239059e19a8cbb28803574316652646c741d583069f1dcfce
|
|
| MD5 |
9260f66b8bf6898ce0037345f605842a
|
|
| BLAKE2b-256 |
a4db6a0c7a73f145df0946448826641c52c1be353f0f99bbb2bbf5817b5ab6ab
|