1👍
✅
as described in documentation
https://www.django-rest-framework.org/api-guide/fields/#serializermethodfield
validation is not working on methodfield because methodfield is read-only field.
0👍
My solution
def get_accounts(self, obj):
if obj.accounts is not None:
return self.validate_accounts(
BankAccountSerializer(obj.accounts, many=True).data
)
return None
def validate_accounts(self, value):
"""
Organization should have account with 'is_main' true
"""
if not value:
return value
for account in value: # caheck all accounts
if account["is_main"] == True:
return value
raise serializers.ValidationError("Organization should have main account (is_main=true)")
Source:stackexchange.com