From 448e9c8f5c6d3f12d17fdb85950577f30435b8e5 Mon Sep 17 00:00:00 2001 From: Herculino Trotta Date: Sun, 8 Nov 2026 17:57:56 -0300 Subject: [PATCH] fix(api): save income categories, tags and entities on account create and update AccountSerializer assigned the many-to-many fields with setattr, which Django rejects, so any write that included them returned a 500. The default ModelSerializer create/update already handles them. --- app/apps/api/serializers/accounts.py | 9 ------- app/apps/api/tests/test_accounts.py | 39 +++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/app/apps/api/serializers/accounts.py b/app/apps/api/serializers/accounts.py index 02082ba1..3345187e 100644 --- a/app/apps/api/serializers/accounts.py +++ b/app/apps/api/serializers/accounts.py @@ -97,15 +97,6 @@ class AccountSerializer(serializers.ModelSerializer): self.fields["group_id"].queryset = AccountGroup.objects.all() self.fields["investment_class_id"].queryset = InvestmentClass.objects.all() - def create(self, validated_data): - return Account.objects.create(**validated_data) - - def update(self, instance, validated_data): - for attr, value in validated_data.items(): - setattr(instance, attr, value) - instance.save() - return instance - class AccountBalanceSerializer(serializers.Serializer): """Serializer for account balance response.""" diff --git a/app/apps/api/tests/test_accounts.py b/app/apps/api/tests/test_accounts.py index 9da60c95..929087b3 100644 --- a/app/apps/api/tests/test_accounts.py +++ b/app/apps/api/tests/test_accounts.py @@ -8,7 +8,7 @@ from rest_framework.test import APIClient from apps.accounts.models import Account, AccountGroup from apps.currencies.models import Currency -from apps.transactions.models import Transaction +from apps.transactions.models import Transaction, TransactionCategory @override_settings( @@ -97,3 +97,40 @@ class AccountBalanceAPITests(TestCase): ) self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) + + def test_patch_and_create_with_income_categories(self): + category = TransactionCategory.objects.create(name="Interest", owner=self.user) + self.account.owner = self.user + self.account.save() + # Writes need Django model permissions + self.user.is_superuser = True + self.user.save() + + response = self.client.patch( + f"/api/accounts/{self.account.id}/", + {"income_categories": [category.id]}, + format="json", + ) + self.assertEqual(response.status_code, status.HTTP_200_OK, response.data) + self.assertEqual(response.data["income_categories"], [category.id]) + # The join table: the related manager is per-user and empty without one + self.assertTrue( + Account.income_categories.through.objects.filter( + account=self.account, transactioncategory=category + ).exists() + ) + + response = self.client.post( + "/api/accounts/", + { + "name": "Broker", + "group_id": None, + "currency_id": self.currency.id, + "exchange_currency_id": None, + "is_investment": True, + "income_categories": [category.id], + }, + format="json", + ) + self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data) + self.assertEqual(response.data["income_categories"], [category.id])