我正在使用flask不安,并试图发布一篇文章,为多对多关系创建一个新记录
Categories = db.Table('categories',
db.Column(
'help_request_id', db.Integer, db.ForeignKey('help_request.id')),
db.Column('category_id', db.Unicode(20), db.ForeignKey('category.id')))
class Category(db.Model):
id = db.Column(db.Unicode(20), primary_key=True)
eng_name = db.Column(db.Unicode(50))
created_by_user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
created_by = db.relationship(User)
create_date = db.Column(db.DateTime, default=datetime.now)
imagePath = db.Column(db.Unicode(128))
# Required for administrative interface
def __unicode__(self):
return self.eng_name
class HelpRequest(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.Unicode(100))
categories = db.relationship('Category', secondary=categories,
backref=db.backref('helprequests', lazy='dynamic'))
如何发布具有指定类别的帮助请求??感谢
Alex
最终解决了问题,只需使用正确的json格式,它就可以像魔术一样工作。
{"title":"testing3","categories":[{"id":"a"},{"id":"b"}]}