2013-10-15 83 views
0

我得到的錯誤爲TypeError: 'Query' object is not callableQuerySelectField不顯示預期的輸出

我在金字塔中創建了一個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() 
+1

當詢問與錯誤有關的問題時,請包含完整的堆棧跟蹤。目前,我們必須猜測你的錯誤發生在哪裏。 –

+1

@MarkHildreth說的。另外,線索是在我打賭的參數名稱中。它應該是query_factory = Category_choice在沒有「()」 –

回答

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詳細說明。

希望它有助於。