Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在棉花糖中打印嵌套模式_Python_Flask_Sqlalchemy_Marshmallow - Fatal编程技术网

Python 在棉花糖中打印嵌套模式

Python 在棉花糖中打印嵌套模式,python,flask,sqlalchemy,marshmallow,Python,Flask,Sqlalchemy,Marshmallow,我使用SQLAlchemy将这些类作为我的模型。我已经成功地让棉花糖编码我的用户对象,但它不会编码嵌套的链接对象。我的SQLAlchemy查询正在fk上执行链接表的联接,该联接工作正常。我很难弄清楚我的NestedSchema出了什么问题,为什么它不会编码 class User(Base): __tablename__ = 'user' id = Column('id', Integer, primary_key=True) username = Column('use

我使用SQLAlchemy将这些类作为我的模型。我已经成功地让棉花糖编码我的用户对象,但它不会编码嵌套的链接对象。我的SQLAlchemy查询正在fk上执行链接表的联接,该联接工作正常。我很难弄清楚我的NestedSchema出了什么问题,为什么它不会编码

class User(Base):
    __tablename__ = 'user'

    id = Column('id', Integer, primary_key=True)
    username = Column('username', String(255), unique=True)
    link = relationship("Link", backref='user', lazy='joined')


class Link(Base):
    __tablename__ = 'link'

    id = Column('id', Integer, primary_key=True)
    name = Column('name', String(255))
    user_id = Column('user_id', Integer, ForeignKey("user.id"), nullable=True)

class LinkSchema(Schema):
    name = fields.Str()
    user = fields.Nested('UserSchema')


class UserSchema(Schema):
    username = fields.Str()
    links = fields.Nested('LinkSchema')
我有一个查询数据库的函数,看起来像这样。这将正确返回用户和链接

def get_user(user_id):
    session = get_session()
    try:
       user = session.query(User).filter(User.id == user_id)
    except exc.SQLAlchemyError:
       return False
   return user
在我看来,这就是我正在执行的。我能够正确地获取我的用户信息,但是棉花糖没有对链接部分进行编码。因为打印链接,我知道它在那里。名称[0]。我已经看了很多次文档,根本不知道哪里出了问题。任何提示都将不胜感激

@app.route('/api/user_info/)
def get_user_info():
    user = get_user(user_id)
    for u in user:
        print (u.link[0].name) #this prints the link name so I know its there
    schema = UserSchema(strict=True, many=True)
    result = schema.dumps(user)
    pprint(result.data, indent=2)
    return result.data
这是返回的json。没有链接!不知道为什么。为了简洁起见,我省略了很多专栏

[{"username": "erick", "role": 1}]

在经历了数小时的挫折之后,我需要在嵌套的UserSchema中将“links”设置为“link”,并且many=True