如何将 Python 对象转换为字符串? [英] How to convert Python Object into String?

查看:59
本文介绍了如何将 Python 对象转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

QuerySelectField is displaying the query correctly but its not saving in the database, it shows the following error while submitting.

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

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

When i enter the title and content and choose the category which is displayed by quering the results from category database and when trying to submit, it throws the above error.

我知道我的错误是这一行

I know my the error is this line

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

我怎么能把它改正,我很想念的地方.

How can i get it correct, where i am missing badly.

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)

创建.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)}

推荐答案

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

The category relationship in your Article model is not correct. It has two problems:

  1. 您尚未提供该列的数据类型.
  2. Article.category 需要是一个关系,而不是一个列.

如果你将你的文章类修改为这样的东西,事情应该会有所改善:

If you modify your Article class to something like this things should improve:

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

如果你这样做,SQLAlchemy 就会知道 category 属性必须是一个 Category 实例,并且会根据需要自动加载和保存 Category 实例.

If you do that SQLAlchemy will know that the category property must be a Category instance and will automatically load and save Category instances as needed.

这篇关于如何将 Python 对象转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆