Flask Restless post 具有多对多关系
Flask Restless post with many to many relationship
我正在使用 flask restless 并尝试做一个 post 来为多对多关系创建一个新记录
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'))
我如何 post 具有指定类别的帮助请求??谢谢
亚历克斯
终于解决了,只要使用正确的 json 格式,它就像魔法一样有效。
{"title":"testing3","categories": [{"id": "a"}, {"id":"b"}]}
我正在使用 flask restless 并尝试做一个 post 来为多对多关系创建一个新记录
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'))
我如何 post 具有指定类别的帮助请求??谢谢
亚历克斯
终于解决了,只要使用正确的 json 格式,它就像魔法一样有效。
{"title":"testing3","categories": [{"id": "a"}, {"id":"b"}]}