0
我得到的錯誤爲TypeError: 'Query' object is not callable
。QuerySelectField不顯示預期的輸出
我在金字塔中創建了一個Blog引擎,並且我被卡在QuerySelectField中,如何顯示分類表中顯示的類別,這些分類是文章模型的一個外鍵?
期望輸出
類別字段應該顯示所有存在於數據庫中選擇類別。
Models.py
DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
Base = declarative_base()
class Category(Base):
__tablename__ = 'categories'
id = Column(Integer, primary_key=True)
name = Column(String(100), unique=True, nullable=False)
description = Column(Text, nullable=False)
class Article(Base):
__tablename__ = 'articles'
id = Column(Integer, primary_key=True)
title = Column(String(255), unique=True, nullable=False)
body = Column(Text, nullable=False)
category = Column(ForeignKey(Category.name), nullable=False)
created = Column(DateTime, default=datetime.datetime.now)
@property
def slug(self):
return urlify(self.title)
@property
def created_in_words(self):
return time_ago_in_words(Article.created)
Views.py
from pyramid.view import view_config
from pyramid.httpexceptions import HTTPFound
import sqlalchemy as sa
from blog.forms import ArticleCreateForm
from blog.models import (
DBSession,
Article, Category,
)
@view_config(route_name='home', renderer='articles/index.jinja2')
def index(request):
articles = DBSession.query(Article).order_by(sa.desc(Article.created))
return {'articles': articles}
@view_config(route_name='article_action', match_param='action=create', renderer='articles/create.jinja2')
def create(request):
article = Article()
form = ArticleCreateForm(request.POST)
if request.method == 'POST' and form.validate():
form.populate_obj(article)
DBSession.add(article)
return HTTPFound(location=request.route_url('home'))
return {'form':form, 'action':request.matchdict.get('create')}
forms.py
from models import DBSession
from models import Category
from wtforms import Form, TextField, TextAreaField, SubmitField, validators
from wtforms.ext.sqlalchemy.fields import QuerySelectField
def Category_choice():
return DBSession.query(Category.name)
class ArticleCreateForm(Form):
title = TextField('Title', [validators.required(), validators.length(min=1,max=240)])
body = TextAreaField('Content', [validators.required()])
category = QuerySelectField('Category', query_factory=Category_choice())
submit = SubmitField()
當詢問與錯誤有關的問題時,請包含完整的堆棧跟蹤。目前,我們必須猜測你的錯誤發生在哪裏。 –
@MarkHildreth說的。另外,線索是在我打賭的參數名稱中。它應該是query_factory = Category_choice在沒有「()」 –