sqlalchemy多对多插入数据
我已经尝试了上面的建议,但是当插入一个带有标签的新帖子时,在一个多对多属性的附加上继续收到以下错误。
任何方向都将不胜感激!
1:06:22 PM web.1|post.tags.append(post_tag)
晚上11:06:22网络。1 |文件“/home/trevor/Projects/trevorbog/venv/local/lib/python2.7/site packages/sqlalchemy/orm/collections.py”,第1072行,在附录中
晚上11:06:22网络。1 |项目=uuu集(自身、项目、发起人)
晚上11:06:22网络。1 |文件“/home/trevor/Projects/trevorbog/venv/local/lib/python2.7/site packages/sqlalchemy/orm/collections.py”,第1044行,在
晚上11:06:22网络。1 |项目=执行人。火灾附加事件(项目,_sa_启动器)
晚上11:06:22网络。1 |文件“/home/trevor/Projects/trevorbog/venv/local/lib/python2.7/site packages/sqlalchemy/orm/collections.py”,第716行,在fire_append_事件中
11:06:22 PM web.1|项目,发起者)
晚上11:06:22网络。1 |文件“/home/trevor/Projects/trevorbog/venv/local/lib/python2.7/site packages/sqlalchemy/orm/attributes.py”,第936行,在fire_append_事件中
晚上11:06:22网络。1 |发起人或自身_附加标记或自我_init_append_token())
晚上11:06:22网络。1 |文件“/home/trevor/Projects/trevorbog/venv/local/lib/python2.7/site packages/sqlalchemy/orm/attributes.py”,第1171行,在emit_backref_from_collection_append_事件中
11:06:22 PM web.1|child_state,child_dict=instance_state(孩子),\
晚上11:06:22网络。1 | AttributeError:“BaseQuery”对象没有属性“_sa_instance_state”
Model.py
blog_tag = db.Table('post_tag',
db.Column('post_id', db.Integer, db.ForeignKey('post.id')),
db.Column('tag_id', db.Integer, db.ForeignKey('tag.id'))
)
class Post(db.Model):
"""SQLAlchemy Post object class"""
id = db.Column(db.Integer, primary_key=True)
body = db.Column(db.Text)
timestamp = db.Column(db.DateTime)
title = db.Column(db.String(50))
author = db.Column(db.Integer, db.ForeignKey('user.id'))
uuid = db.Column(db.String(255))
tags = db.relationship('Tag',secondary=blog_tag,
back_populates="posts")
class Tag(db.Model):
"""SQLAlchemy Tag object class"""
id=db.Column(db.Integer, primary_key=True)
name=db.Column(db.String, unique=True, nullable=False)
posts = db.relationship('Post', secondary = blog_tag,
back_populates = "tags")
Views.py
@console.route('/new_post', methods=['GET', 'POST'])
@login_required
def new_post():
post_form = PostForm()
if post_form.validate_on_submit():
"""Take the list of tags and turn them into an array"""
uid = uuid.uuid4()
post = Post()
post.title = post_form.title.data
post.body = post_form.body.data
post.timestamp = datetime.utcnow()
post.author = current_user.id
post.uuid = uid.hex
tag_string = post_form.tags.data
tags = tag_string.split(",")
for tag in tags:
post_tag = add_tags(tag)
print post_tag
post.tags.append(post_tag)
db.session.add(post)
db.session.commit()
flash (u'New Post Created!', 'alert-info')
return render_template('new_post.html',
title='Admin-New Post',
form=post_form)
def add_tags(tag):
existing_tag = Tag.query.filter(Tag.name == tag.lower())
"""if it does return existing tag objec to list"""
if existing_tag is not None:
return existing_tag
else:
new_tag = Tag()
new_tag.name = tag.lower()
return new_tag
这个:
existing_tag = Tag.query.filter(Tag.name == tag.lower())
"""if it does return existing tag objec to list"""
if existing_tag is not None:
return existing_tag
不返回标记
,而是返回查询
。您想要标记。查询过滤器(…)。一个\u或\u none()
来获取标记本身。