AttributeError:'int'对象没有属性'_sa_instance_state' [英] AttributeError: 'int' object has no attribute '_sa_instance_state'

查看:4057
本文介绍了AttributeError:'int'对象没有属性'_sa_instance_state'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flask开发论坛模板。当我尝试使用窗体在浏览器中创建一个新的线程时,SQLAlchemy抛出一个AttributeError。当我尝试实现与Forum-to-Thread的一对多关系以及与Thread-to-User的一对多关系时,问题就出现了。



models.py

  class User(db.Model):
id = db.Column(db.Integer, primary_key = True)
username = db.Column(db.String(32),index = True,unique = True)
password = db.Column(db.String(32),index = True)
email = db.Column(db.String(120),index = True,unique = True)
role = db.Column(db.SmallInteger,default = ROLE_USER)
$ b $论坛(db.Model):
id = db.Column(db); db.relationship('Post',backref ='author',lazy ='dynamic')
$ b $ .Integer,primary_key = True)
title = db.Column(db.String(128))
description = db.Column(db.Text)

threads = db。关系('Thread',backref ='forum',lazy ='dynamic')

class线程(db.Model):

id = db.Column(db。整数,primary_key = True)
title = db.Column(db.S tring(128))
author = db.Column(db.String(32))
timestamp = db.Column(db.DateTime)
forum_id = db.Column(db.Integer, db.ForeignKey('forum.id'))

posts = db.relationship('Post',backref ='thread',lazy ='dynamic')

class Post(db.Model):

id = db.Column(db.Integer,primary_key = True)
body = db.Column(db.Text)
timestamp = db .Column(db.DateTime)
thread_id = db.Column(db.Integer,db.ForeignKey('thread.id'))
user_id = db.Column(db.Integer,db.ForeignKey 'user.id'))

所有新帖子/ p>

views.py

  @ app.route('/ forum / id =< id> / submit',methods = ['GET','POST'])
@login_required $ b $ def new_thread(id):
form = ThreadForm()
论坛= Forum.query.filter_by(id = id).first()
如果form.validate_on_submit():
thread = Thread(title = form.title.data,
作者= g.user.username,
timestamp = datetime.utcnow())
db.session.add(线程)
db.session.flush()
post = Post (body = form.body.data,
timestamp = datetime.utcnow(),
thread = thread.id,
author = g.user.id)
db.session .add(post)
db.session.commit()
flash('Post successful。')
返回重定向(url_for('forum_index',id = id))
返回render_template('forum / thread_submit.html',title = forum.title,form = form)


解决方案

问题是这样的:

  post = Post(body = form.body.data ,
timestamp = datetime.utcnow(),
thread = thread.id,
author = g.user.id)

要使用ORM对象而不是主键列:

  post = Post(body = form.body.data,
timestamp = datetime.utcnow(),
thread = thread,$ b $ author = g.user)

错误意味着一个整数被解释为一个ORM对象。


I'm working on forum template using Flask. When I attempt creating a new thread in the browser using forms, SQLAlchemy throws an AttributeError. The problem showed up when I tried implementing a one-to-many relationship with Forum-to-Thread and a one-to-many relationship with Thread-to-User.

models.py

class User(db.Model):
  id = db.Column(db.Integer, primary_key=True)
  username = db.Column(db.String(32), index=True, unique=True)
  password = db.Column(db.String(32), index=True)
  email = db.Column(db.String(120), index=True, unique=True)
  role = db.Column(db.SmallInteger, default=ROLE_USER)

  posts = db.relationship('Post', backref='author', lazy='dynamic')

class Forum(db.Model):
  id = db.Column(db.Integer, primary_key=True)
  title = db.Column(db.String(128))
  description = db.Column(db.Text)

  threads = db.relationship('Thread', backref='forum', lazy='dynamic')

class Thread(db.Model):

  id = db.Column(db.Integer, primary_key=True)
  title = db.Column(db.String(128))
  author= db.Column(db.String(32))
  timestamp = db.Column(db.DateTime)
  forum_id = db.Column(db.Integer, db.ForeignKey('forum.id'))

  posts = db.relationship('Post', backref='thread', lazy='dynamic')

class Post(db.Model):

  id = db.Column(db.Integer, primary_key=True)
  body = db.Column(db.Text)
  timestamp = db.Column(db.DateTime)
  thread_id = db.Column(db.Integer, db.ForeignKey('thread.id'))
  user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

All the new posts/threads and handled within views.py

views.py

@app.route('/forum/id=<id>/submit', methods=['GET','POST'])
@login_required
def new_thread(id):
  form = ThreadForm()
  forum = Forum.query.filter_by(id=id).first()
  if form.validate_on_submit():
    thread = Thread(title=form.title.data,
                    author=g.user.username,
                    timestamp=datetime.utcnow())
    db.session.add(thread)
    db.session.flush()
    post = Post(body=form.body.data,
                timestamp=datetime.utcnow(),
                thread=thread.id,
                author=g.user.id)
    db.session.add(post)
    db.session.commit()
    flash('Post successful.')
    return redirect(url_for('forum_index', id=id))
  return render_template('forum/thread_submit.html', title=forum.title, form=form) 

解决方案

the problem is this:

post = Post(body=form.body.data,
            timestamp=datetime.utcnow(),
            thread=thread.id,
            author=g.user.id)

you want to work with ORM objects, not primary key columns:

post = Post(body=form.body.data,
            timestamp=datetime.utcnow(),
            thread=thread,
            author=g.user)

the error means that an integer is being interpreted as an ORM object.

这篇关于AttributeError:'int'对象没有属性'_sa_instance_state'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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