From b0b189772ea545be00a3ac2d13584aee33400318 Mon Sep 17 00:00:00 2001 From: tkdwns414 Date: Thu, 17 Aug 2023 15:24:43 +0900 Subject: [PATCH] add level property in Profile & add in Profile view --- accounts/models.py | 6 ++++++ accounts/serializers.py | 2 ++ core/serializers.py | 7 +------ 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/accounts/models.py b/accounts/models.py index e3d64bb..7dc895b 100644 --- a/accounts/models.py +++ b/accounts/models.py @@ -20,6 +20,12 @@ class Profile(BaseModel): info = models.JSONField(default={}) completed_cnt = models.IntegerField(default=0) + @property + def level(self): + level = self.completed_cnt // 3 + 1 + level = 5 if (level > 5) else level + return level + class ProfileReport(ReportBaseModel): profile = models.ForeignKey( diff --git a/accounts/serializers.py b/accounts/serializers.py index c43bd6e..a194d97 100644 --- a/accounts/serializers.py +++ b/accounts/serializers.py @@ -20,6 +20,7 @@ class Meta: "type", "user_id", "joined_at", + "level", ] read_only_fields = [ "id", @@ -29,6 +30,7 @@ class Meta: "completed_cnt", "type", "user_id", + "level", ] def get_following_cnt(self, instance): diff --git a/core/serializers.py b/core/serializers.py index 98a6e87..4643259 100644 --- a/core/serializers.py +++ b/core/serializers.py @@ -7,12 +7,7 @@ class WriterSerializer(serializers.ModelSerializer): profile_id = serializers.IntegerField(source="profile.id") nickname = serializers.CharField(source="profile.nickname") type = serializers.CharField(source="profile.type") - level = serializers.SerializerMethodField(read_only=True) - - def get_level(self, instance): - level = instance.profile.completed_cnt // 3 + 1 - level = 5 if (level > 5) else level - return level + level = serializers.IntegerField(source="profile.level") class Meta: model = User