Fix: title in post serializers
This commit is contained in:
parent
f3d67d8f97
commit
9a2759bb21
1 changed files with 17 additions and 0 deletions
|
@ -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 == "":
|
||||
|
|
Loading…
Add table
Reference in a new issue