TypeError:update()至少需要2个参数(给定1个)

时间:2018-02-24 23:01:45

标签: javascript python flask-sqlalchemy webpage

我正在使用带有Flask-SQLALCHEMY的REST API构建一个网站。 我一直在尝试为特定的API添加更新,它总是告诉我它需要2个参数,我给它一个。

Views.py代码

    def patch(self, groupnumber):
    group_query = Group.query.filter_by(groupnumber=groupnumber)
    raw_dict = request.form
    try:
        schemaGroup.validate(raw_dict)
        for key, value in raw_dict.items():
            setattr(group_query, key, value)

        group_query.update()
        return self.get(userid)

    except ValidationError as err:
        resp = jsonify({"error": err.messages})
        resp.status_code = 401
        return resp

    except SQLAlchemyError as e:
        db.session.rollback()
        resp = jsonify({"error": str(e)})
        resp.status_code = 401
        return resp

Models.py代码

class Group(db.Model, CRUD):
__tablename__ = 'cowgroup'
cownumber = db.Column(db.Text)
groupnumber = db.Column(db.Integer, primary_key=True)
groupname = db.Column(db.Text)
groupdescription = db.Column(db.Text)
attributes = db.Column(db.Text)
user = db.Column(db.Text)

def __init__(self, cownumber, groupnumber, groupname, groupdescription, attributes,user):
    self.cownumber = cownumber
    self.groupnumber = groupnumber
    self.groupname = groupname
    self.groupdescription = groupdescription
    self.attributes = str(attributes)
    self.user = user


class Group_Schema(Schema):
    not_blank = validate.Length(min=1, error='Field cannot be blank')
    id = fields.Integer(dump_only=True)  # WHY DOES THIS HAVE TO BE HERE???
    cownumber = fields.String(validate=not_blank)
    groupnumber = fields.Integer(validate=not_blank)
    groupname = fields.String()
    groupdescription = fields.String()
    attributes = fields.String()
    user = fields.String()

    # self links
    def get_top_level_links(self, data, many):
        if many:
            self_link = "/group/"
        else:
            self_link = "/group/{}".format(data['attributes']['groupnumber'])
        return {"self": self_link}

    class Meta:
        type_ = 'group'

您可以在此处查看我将要传递的数据的架构,我还会将我正在通过的数据放在此处。

"cownumber": "[\"365\",\"366\",\"367\"]",
"groupnumber": 1011,
"groupname": "Test1",
"user": "pk@gmail.com",
"attributes": "[\"pasturenumber\",\"sex\",\"breed\",\"height\",\"weight\",\"animaltype\",\"status\"]",
"groupdescription": "Test1"

有人可以调查一下并帮助我做错了什么。 我找不到任何涉及update()的相关主题。

1 个答案:

答案 0 :(得分:0)

根据文档,Query.update需要1个强制位置参数,该参数应该是要更新的属性的dict

您应该按以下方式更新代码:

# ...
schemaGroup.validate(raw_dict)
group_query.update(raw_dict)
return self.get(userid)
# ...