Merge pull request #254

fix(api:accounts): unable to create an account with an account group
This commit is contained in:
Herculino Trotta
2025-05-11 12:47:23 -03:00
committed by GitHub
2 changed files with 13 additions and 1 deletions

View File

@@ -41,7 +41,10 @@ class TransactionCategoryField(serializers.Field):
def get_schema(): def get_schema():
return { return {
"type": "array", "type": "array",
"items": {"type": "string", "description": "TransactionTag ID or name"}, "items": {
"type": "string",
"description": "TransactionCategory ID or name",
},
} }

View File

@@ -1,3 +1,4 @@
from django.db.models import Q
from rest_framework import serializers from rest_framework import serializers
from rest_framework.permissions import IsAuthenticated from rest_framework.permissions import IsAuthenticated
@@ -22,6 +23,7 @@ class AccountSerializer(serializers.ModelSerializer):
write_only=True, write_only=True,
allow_null=True, allow_null=True,
) )
currency = CurrencySerializer(read_only=True) currency = CurrencySerializer(read_only=True)
currency_id = serializers.PrimaryKeyRelatedField( currency_id = serializers.PrimaryKeyRelatedField(
queryset=Currency.objects.all(), source="currency", write_only=True queryset=Currency.objects.all(), source="currency", write_only=True
@@ -50,6 +52,13 @@ class AccountSerializer(serializers.ModelSerializer):
"is_asset", "is_asset",
] ]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
request = self.context.get("request")
if request and request.user.is_authenticated:
# Reload the queryset to get an updated version with the requesting user
self.fields["group_id"].queryset = AccountGroup.objects.all()
def create(self, validated_data): def create(self, validated_data):
return Account.objects.create(**validated_data) return Account.objects.create(**validated_data)