From 78a3f66d8ce6e07243e389a00c8379e7622cad5b Mon Sep 17 00:00:00 2001 From: YeonJeong Kim Date: Sun, 10 Dec 2023 12:34:17 +0900 Subject: [PATCH] :bug: Fix bugs - didn't delete weight rows in db when symbols are deleted --- backend/weight_table/views.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/backend/weight_table/views.py b/backend/weight_table/views.py index 59b604bb..d8a880e5 100644 --- a/backend/weight_table/views.py +++ b/backend/weight_table/views.py @@ -17,6 +17,19 @@ def post(self, request): serializer.is_valid(raise_exception=True) serializer.save() + # Delete weight rows if user deleted corresponding symbols + weight_symbols = WeightTable.objects.filter(user=user).values('symbol_id') + weight_to_delete = [item['symbol_id'] for item in weight_symbols] + + try: + for item in serializer.validated_data: + weight_to_delete.remove(item.get('symbol_id')) + except ValueError: + pass + + for id in weight_to_delete: + WeightTable.objects.get(symbol_id=id, user=user).delete() + return Response(status=status.HTTP_200_OK) def get(self, request):