1

由于某种原因,此代码会产生以下错误,我无法弄清楚原因。据我所知,在我遵循的指南中,dump 的调用方式与谷歌搜索没有结果,尤其是因为这似乎是 json.dump/json.dumps 也可能产生的错误消息。任何帮助将不胜感激。

from flask_marshmallow import fields
from app import db, ma



class Person(db.Model):
    __tablename__ = "person"
    person_id = db.Column(db.Integer, primary_key=True)
    # 1 to 1

    personal_nr = db.Column(db.Integer)
    usr_name = db.Column(db.String(32))
    first_name = db.Column(db.String(32))
    last_name = db.Column(db.String(32))
    birthday = db.Column(db.DateTime)

    person_type = db.Column(db.String(32))

    __mapper_args__ = {
        'polymorphic_identity': 'person',
        'polymorphic_on': person_type
    }


class PersonSchema(ma.Schema):
    class Meta:
        model = Person
        sqla_session = db.session


class User(Person):
    __tablename__ = "user"
    password_hash = db.Column(db.String(65))

    __mapper_args__ = {
        'polymorphic_identity': 'user',
    }


class UserSchema(PersonSchema):
    password_hash = fields.fields.String()


nutzer = User

nutzer.usr_name = 'testnutzer'
nutzer.password_hash = 'rfsedlujhnogiyefdgxrjuvbhklnrf'
nutzer.personal_nr = 1234
nutzer.first_name = 'fritz'
nutzer.last_name = 'müller'
nutzer_schema = UserSchema



def dumper():
    return nutzer_schema.dumps(nutzer)


dumper()

TypeError                                 Traceback (most recent call last)

<ipython-input-1-cf86950183fb> in <module>
     57 
     58 
---> 59 dumper()

<ipython-input-1-cf86950183fb> in dumper()
     54 
     55 def dumper():
---> 56     return nutzer_schema.dumps(nutzer)
     57 
     58 

TypeError: dumps() missing 1 required positional argument: 'obj'

如果使用 .dump 而不是 .dumps,则错误是相同的。不在 jupyter 笔记本中时,错误保持不变。

4

1 回答 1

2

我相信您忘记实例化用户对象和架构。替换nutzer = Usernutzer = User()和。nutzer_schema = UserSchema_nutzer_schema = UserSchema()

此外,建议将对象作为参数传递给函数:

def dumper(obj):
    return nutzer_schema.dumps(obj)


dumper(nutzer)
于 2021-06-10T12:16:57.453 回答