Skip to content

Commit

Permalink
Fixed query overhead in QuerySelectField
Browse files Browse the repository at this point in the history
  • Loading branch information
n.lyubchich committed Aug 13, 2015
1 parent e8cc425 commit 0ad415f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
17 changes: 11 additions & 6 deletions project/pages/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@


class PageDetail(EntryDetail):
def _provide_pageblocks(self, form):
pageblocks = PageBlock.query.all()
for block in form.blocks:
setattr(block, "query", pageblocks)

def get(self, entry_id):

if entry_id is None:
# Restrict creating new pages
abort(403)
Expand All @@ -16,15 +22,12 @@ def get(self, entry_id):

if entry is None:
abort(404)
entry_form = self.update_form(obj=entry)

pageblocks = [block.title for block in PageBlock.query.all()]

for block in entry_form.blocks:
block.choices = pageblocks
form = self.update_form(obj=entry)
self._provide_pageblocks(form)

return self.render_response(
entry_form=entry_form,
entry_form=form,
entry=entry)

def post(self, entry_id):
Expand All @@ -34,6 +37,8 @@ def post(self, entry_id):

# Update an old entry
form = self.update_form()
self._provide_pageblocks(form)

if form.validate_on_submit():
instance = self.model.bl.get(entry_id)
instance.bl.update(form.data)
Expand Down
3 changes: 2 additions & 1 deletion project/pages/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from flask_wtf import Form
from wtforms import StringField, TextAreaField, SelectField, FieldList
from wtforms.ext.sqlalchemy.fields import QuerySelectField
from wtforms.validators import Length
from project.models import PageBlock

Expand Down Expand Up @@ -52,6 +53,6 @@ class PageForm(Form):
],
)
blocks = FieldList(
SelectField(), # choices should be provided in view!
QuerySelectField(), # query should be provided in view
min_entries=1,
)

0 comments on commit 0ad415f

Please sign in to comment.