私はフラスコの安静な API と peewee を使用した SQLite データベースを作成しました。データを保存しているアート作品のリストを「取得」できます。また、問題なく「取得」、「投稿」、および「配置」することもできます。ただし、単一のピースを削除する場合、API はすべてのピース エントリを削除します。現在、郵便配達員を使用して API をテストしているだけなので、AJAX または JavaScript のエラーではないことがわかっています (これについては後で書きます)。どんなガイダンスも役立つかもしれません。
削除要求を行うために必要なクエリの数を増やそうとしました。ここで、データベースの created_by フィールド (整数 ID) は、認証しているユーザーの ID と一致する必要があります。2 人のユーザーを作成し、それぞれ 2 つの異なる作品を投稿しましたが、作品に対して削除要求を実行しても、すべての作品が削除されました。
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 のピースのみが削除されます。