mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-04-24 09:38:35 +02:00
Merge pull request #428 from SerafimPikalov/fix/null-category-serialization
fix: handle null category in TransactionCategoryField serialization
This commit is contained in:
@@ -10,15 +10,19 @@ from apps.transactions.models import (
|
|||||||
|
|
||||||
@extend_schema_field(
|
@extend_schema_field(
|
||||||
{
|
{
|
||||||
"oneOf": [{"type": "string"}, {"type": "integer"}],
|
"oneOf": [{"type": "string"}, {"type": "integer"}, {"type": "null"}],
|
||||||
"description": "TransactionCategory ID or name. If the name doesn't exist, a new one will be created",
|
"description": "TransactionCategory ID or name. If the name doesn't exist, a new one will be created. Can be null if no category is assigned.",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
class TransactionCategoryField(serializers.Field):
|
class TransactionCategoryField(serializers.Field):
|
||||||
def to_representation(self, value):
|
def to_representation(self, value):
|
||||||
|
if value is None:
|
||||||
|
return None
|
||||||
return {"id": value.id, "name": value.name}
|
return {"id": value.id, "name": value.name}
|
||||||
|
|
||||||
def to_internal_value(self, data):
|
def to_internal_value(self, data):
|
||||||
|
if data is None:
|
||||||
|
return None
|
||||||
if isinstance(data, int):
|
if isinstance(data, int):
|
||||||
try:
|
try:
|
||||||
return TransactionCategory.objects.get(pk=data)
|
return TransactionCategory.objects.get(pk=data)
|
||||||
|
|||||||
Reference in New Issue
Block a user