Fix: title in post serializers

This commit is contained in:
Aroy-Art 2025-03-18 22:05:12 +01:00
parent f3d67d8f97
commit 9a2759bb21
Signed by: Aroy
GPG key ID: 583642324A1D2070

View file

@ -8,6 +8,7 @@ from apps.archive.models import PostModel
class PostPreviewSerializer(serializers.ModelSerializer):
title = serializers.SerializerMethodField()
description = serializers.SerializerMethodField()
date = serializers.SerializerMethodField()
media = serializers.SerializerMethodField()
@ -33,6 +34,9 @@ class PostPreviewSerializer(serializers.ModelSerializer):
"tags",
]
def get_title(self, obj: PostModel) -> Optional[str]:
return obj.title.first().content if obj.title.exists() else None
def get_description(self, obj: PostModel) -> Optional[str]:
return obj.description.first().content if obj.description.exists() else None
@ -84,6 +88,7 @@ class PostPreviewSerializer(serializers.ModelSerializer):
class PostSerializer(serializers.ModelSerializer):
source_site = serializers.SerializerMethodField()
title = serializers.SerializerMethodField()
description = serializers.SerializerMethodField()
creator = serializers.SerializerMethodField()
tags = serializers.SerializerMethodField()
@ -110,6 +115,18 @@ class PostSerializer(serializers.ModelSerializer):
"name": obj.source_site.name,
}
def get_title(self, obj) -> Dict[str, Optional[str]]:
title = obj.title.first().content if obj.title.exists() else None
if not title:
count = 0
else:
count = obj.title.count()
return {
"count": count,
"content": title,
}
def get_description(self, obj) -> Dict[str, str]:
desc = obj.description.first().content
if desc == "":