QuerySelectField不显示预期输出

时间:2013-10-15 05:55:47

标签: python sqlalchemy pyramid wtforms

我收到错误为TypeError: 'Query' object is not callable

我在金字塔中创建一个博客引擎,我被困在QuerySelectField中,如何显示单独的表中的类别,这些类别是一个ForeignKey到文章模型?

预期输出

  

类别字段应显示数据库中存在的所有类别以供选择。

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()

1 个答案:

答案 0 :(得分:1)

将此行从query_factory=Category_choice()更改为query_factory=Category_choice

Category_choice 功能更改为

def Category_choice():
    return DBSession.query(Category)

所以你的表格现在看起来像这样

def Category_choice():
    return DBSession.query(Category)

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()

在您的分类模型中添加此方法调用

   def __str__(self):
        return self.name

请参阅This Documentation详细说明。

希望它有所帮助。