From 6dc14c73d6484ca180b1bc85ef015a619a53ab8b Mon Sep 17 00:00:00 2001 From: Herculino Trotta Date: Sun, 11 May 2025 12:41:26 -0300 Subject: [PATCH] fix(api:accounts): unable to create an account with an account group Fixes #253 --- app/apps/api/serializers/accounts.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/apps/api/serializers/accounts.py b/app/apps/api/serializers/accounts.py index afd59b1..c7f5e45 100644 --- a/app/apps/api/serializers/accounts.py +++ b/app/apps/api/serializers/accounts.py @@ -1,3 +1,4 @@ +from django.db.models import Q from rest_framework import serializers from rest_framework.permissions import IsAuthenticated @@ -22,6 +23,7 @@ class AccountSerializer(serializers.ModelSerializer): write_only=True, allow_null=True, ) + currency = CurrencySerializer(read_only=True) currency_id = serializers.PrimaryKeyRelatedField( queryset=Currency.objects.all(), source="currency", write_only=True @@ -50,6 +52,13 @@ class AccountSerializer(serializers.ModelSerializer): "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): return Account.objects.create(**validated_data)