由于某种原因,此代码会产生以下错误,我无法弄清楚原因。据我所知,在我遵循的指南中,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 笔记本中时,错误保持不变。