Python Flask-Restful 错误 : Delete method in API is deleting ALL database entries

标签 python sqlite flask flask-restful flask-peewee

我已经编写了一个 Flask-restful API,以及一个使用 peewee 的 SQLite 数据库。我能够“获取”我正在为其存储数据的艺术品列表。我还能够毫无问题地“获取”单件、“发布”和“放置”。但是,如果我想删除单个片段,我的 API 会删除所有片段条目。现在我只是使用 postman 测试我的 API,所以我知道这不是 AJAX 或 javascript 错误(我稍后会写)。任何指导可能会有帮助。

我尝试增加发出删除请求所需的查询数量,其中数据库中的created_by字段(这是一个整数id)必须与进行身份验证的用户id相匹配。我创建了两个用户并分别发布了两个不同的片段,并对一个片段运行删除请求仍然删除了所有片段。

def piece_or_404(id):
    try:
        piece = models.Piece.get(models.Piece.id==id)
    except models.Piece.DoesNotExist:
        abort(404)
    else:
        return piece
class Piece(Resource):
    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument(
            'title',
            required=True,
            help='No title provided',
            location=['form', 'json']
        )
        self.reqparse.add_argument(
            'location',
            required=True,
            help='No url provided',
            location=['form', 'json']
        )
        self.reqparse.add_argument(
            'description',
            required=False,
            nullable=True,
            location=['form', 'json'],
        )
        self.reqparse.add_argument(
            'created',
            type=inputs.date,
            required=False,
            help='Date not in YYYY-mm-dd format',
            location=['form', 'json']
        )
        self.reqparse.add_argument(
            'price',
            type=inputs.positive,
            required=True,
            help='No price provided',
            location=['form', 'json']
        )
        self.reqparse.add_argument(
            'created_by',
            type=inputs.positive,
            required=True,
            help='No user provided',
            location=['form', 'json']
        )
        super().__init__()

    @auth.login_required
    def delete(self, id):
        try:
            Piece = models.Piece.select().where(
                models.Piece.id==id
            ).get()
        except models.Piece.DoesNotExist:
            return make_response(json.dumps(
                    {'error': 'That Piece does not exist or is not editable'}
                ), 403)
        query = Piece.delete()
        query.execute()
        return '', 204, {'Location': url_for('resources.pieces.pieces')}

如果我有 id 为 1、2 和 3 的片段,那么在 url.com/api/v1/pieces/1 上运行有效的删除请求,应该只会删除 id 为 1 的片段。

最佳答案

问题是您在实例上使用表级方法delete()。您还可以使用行级方法 delete_instance()。请参阅:http://docs.peewee-orm.com/en/latest/peewee/api.html#Model

对于如何解决此问题,您有两种选择:

1 更改对删除的调用,以添加与选择匹配的位置。

query = models.Piece.delete().where(models.Piece.id==id)
query.execute()

参见http://docs.peewee-orm.com/en/latest/peewee/api.html#Model.delete (注意警告!)

2 在对象实例上使用 delete_instance() 方法:

Piece.delete_instance()

参见http://docs.peewee-orm.com/en/latest/peewee/api.html#Model.delete_instance

关于Python Flask-Restful 错误 : Delete method in API is deleting ALL database entries,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55820144/

相关文章:

python - 排列不起作用

angular - 将 Ionic 存储项作为字符串获取

python - 在 SQLAlchemy 关系上设置 delete-orphan 会导致 AssertionError : This AttributeImpl is not configured to track parents

android - 滥用聚合函数 MAX()

c# - 如何从 C# 在 SQLite DB 中插入和查询 DateTime 对象?

python - 如何在没有路由的情况下在Python Flask中执行普通函数并使用路由返回该过程的结果?

python - 值错误 : too many values to unpack

php - 我可以在 Google App Engine for Python 上运行 PHP 文件吗?

python - 用Python通过api访问Json数据

python - Py_InitModule 复制名称但不复制函数指针?