如何将Python对象转换为字符串?

时间:2013-10-16 08:46:59

标签: python sqlalchemy pyramid

QuerySelectField 正在正确显示查询,但未在数据库中保存,在提交时显示以下错误。

ProgrammingError: (ProgrammingError) can't adapt type 'Category' 'INSERT INTO articles (title, body, category, created) VALUES (%(title)s, %(body)s, %(category)s, %(created)s) RETURNING articles.id' {'category': <blog.models.Category object at 0x03A7E2D0>, 'body': u'Test', 'title': u'This is the New Post', 'created': datetime.datetime(2013, 10, 16, 14, 7, 23, 508000)}

enter image description here

当我输入标题和内容并选择通过查询类别数据库中的结果显示的类别时以及尝试提交时,它会抛出上述错误。

我知道我的错误是这一行

{'category': <blog.models.Category object at 0x03A7E2D0>

我怎样才能把它弄错,我错过了哪里。


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)

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


class CategoryCreateForm(Form):
    name = TextField('Name', [validators.required(), validators.length(min=1,max=240)])
    description = TextAreaField('Description', [validators.required()])
    submit = SubmitField()

Views.py

@view_config(route_name='article_action', match_param='action=create', renderer='articles/create.jinja2')
def create(request):
    article = Article()
    category = Category()
    form = ArticleCreateForm(request.POST)
    if request.method == 'POST' and form.validate():
        form.populate_obj(article )
        DBSession.add(article, category)
        return HTTPFound(location=request.route_url('home'))
    return {'form':form, 'action':request.matchdict.get('create')}

Models.py

class Category(Base):
    __tablename__ = 'categories'
    id = Column(Integer, primary_key=True)
    name = Column(String(100), unique=True, nullable=False)
    description = Column(Text, nullable=False)

    def __unicode__(self):
        return self.name

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(self.created)

Create.jinja2

{% extends 'base.jinja2' %}

{% block content %}
    <form action="{{ request.route_url('article_action', action='create') }}" method="post">
        {{ form.title.label }}
        {{ form.title }}<br/>

        {{ form.body.label }}
        {{ form.body }}<br/>

        {{ form.category.label }}
        {{ form.category }}<br/>

        {{ form.submit }}
    </form>
        <a href="{{ request.route_url('home') }}">Home</a>
{% endblock %}

完整追溯

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\pyramid_debugtoolbar\panels\performance.py", line 69, in noresource_timer_handler
    result = handler(request)
  File "C:\Python27\lib\site-packages\pyramid\tweens.py", line 21, in excview_tween
    response = handler(request)
  File "C:\Python27\lib\site-packages\pyramid_tm\__init__.py", line 82, in tm_tween
    reraise(*exc_info)
  File "C:\Python27\lib\site-packages\pyramid_tm\__init__.py", line 70, in tm_tween
    manager.commit()
  File "C:\Python27\lib\site-packages\transaction\_manager.py", line 111, in commit
    return self.get().commit()
  File "C:\Python27\lib\site-packages\transaction\_transaction.py", line 280, in commit
    reraise(t, v, tb)
  File "C:\Python27\lib\site-packages\transaction\_transaction.py", line 271, in commit
    self._commitResources()
  File "C:\Python27\lib\site-packages\transaction\_transaction.py", line 417, in _commitResources
    reraise(t, v, tb)
  File "C:\Python27\lib\site-packages\transaction\_transaction.py", line 389, in _commitResources
    rm.tpc_begin(self)
  File "C:\Python27\lib\site-packages\zope\sqlalchemy\datamanager.py", line 90, in tpc_begin
    self.session.flush()
  File "C:\Python27\lib\site-packages\sqlalchemy\orm\session.py", line 1818, in flush
    self._flush(objects)
  File "C:\Python27\lib\site-packages\sqlalchemy\orm\session.py", line 1936, in _flush
    transaction.rollback(_capture_exception=True)
  File "C:\Python27\lib\site-packages\sqlalchemy\util\langhelpers.py", line 58, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "C:\Python27\lib\site-packages\sqlalchemy\orm\session.py", line 1900, in _flush
    flush_context.execute()
  File "C:\Python27\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 372, in execute
    rec.execute(self)
  File "C:\Python27\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 525, in execute
    uow
  File "C:\Python27\lib\site-packages\sqlalchemy\orm\persistence.py", line 64, in save_obj
    table, insert)
  File "C:\Python27\lib\site-packages\sqlalchemy\orm\persistence.py", line 569, in _emit_insert_statements
    execute(statement, params)
  File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 662, in execute
    params)
  File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 761, in _execute_clauseelement
    compiled_sql, distilled_params
  File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 874, in _execute_context
    context)
  File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 1024, in _handle_dbapi_exception
    exc_info
  File "C:\Python27\lib\site-packages\sqlalchemy\util\compat.py", line 195, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb)
  File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 867, in _execute_context
    context)
  File "C:\Python27\lib\site-packages\sqlalchemy\engine\default.py", line 324, in do_execute
    cursor.execute(statement, parameters)
ProgrammingError: (ProgrammingError) can't adapt type 'Category' 'INSERT INTO articles (title, body, category, created) VALUES (%(title)s, %(body)s, %(category)s, %(created)s) RETURNING articles.id' {'category': <blog.models.Category object at 0x03A7E2D0>, 'body': u'Test', 'title': u'This is the New Post', 'created': datetime.datetime(2013, 10, 16, 14, 7, 23, 508000)}

3 个答案:

答案 0 :(得分:3)

你的问题在于:

form.populate_obj(article)

这基本上将表单字段值分配给数据库对象的相应字段。但是,您没有使用SQLAlchemy relationship(您应该!),因此article.category需要包含该类别的名称(为什么不是ID?!)。但是,QuerySelectField具有数据库对象(即Category实例)作为其值。

你有三种选择;第三个是首选的:

  • 使用从数据库填充choices的普通SelectField
  • 请勿使用populate_obj,而是使用

    手动分配类别
    article.category = form.category.data.name
    
  • category中的Article列重命名为category_name并使用relationship(),因此Article.category不包含名称,只包含Category个对象。

答案 1 :(得分:1)

文章模型中的类别关系不正确。它有两个问题:

  1. 您尚未提供该列的数据类型。
  2. Article.category必须是关系,而不是列。
  3. 如果您将Article类修改为类似的东西,则应该改进:

    class Article(Base):
        ...
        category_name = Column(String(100), ForeignKey(Category.name), nullable=False)
        category = relationship(Category)
    

    如果你这样做,SQLAlchemy将知道category属性必须是Category实例,并将根据需要自动加载和保存Category实例。

答案 2 :(得分:1)

向模型字段添加关系。