From 4f139f00db1c6502a6b6b5f0b226f0675dc072df Mon Sep 17 00:00:00 2001 From: rosiemaguire Date: Tue, 5 Sep 2023 20:11:05 +1000 Subject: [PATCH] Returning project owner & supporter names rather than IDs in serializer --- crowdfunding/projects/models.py | 2 +- crowdfunding/projects/serializers.py | 2 ++ crowdfunding/users/models.py | 9 ++++++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/crowdfunding/projects/models.py b/crowdfunding/projects/models.py index 7688cef..d6b66fb 100644 --- a/crowdfunding/projects/models.py +++ b/crowdfunding/projects/models.py @@ -12,7 +12,7 @@ class Project(models.Model): owner = models.ForeignKey( get_user_model(), on_delete=models.CASCADE, - related_name='owned_projects' + related_name='owner_projects' ) is_deleted = models.BooleanField(default=False) diff --git a/crowdfunding/projects/serializers.py b/crowdfunding/projects/serializers.py index 3e56284..2425c7e 100644 --- a/crowdfunding/projects/serializers.py +++ b/crowdfunding/projects/serializers.py @@ -3,12 +3,14 @@ class PledgeSerializer(serializers.ModelSerializer): supporter = serializers.ReadOnlyField(source='supporter.id') + supporter = serializers.StringRelatedField() class Meta: model = apps.get_model('projects.Pledge') fields = '__all__' class ProjectSerializer(serializers.ModelSerializer): owner = serializers.ReadOnlyField(source='owner.id') + owner = serializers.StringRelatedField() class Meta: model = apps.get_model('projects.Project') fields = '__all__' diff --git a/crowdfunding/users/models.py b/crowdfunding/users/models.py index 8a5ba11..5015c12 100644 --- a/crowdfunding/users/models.py +++ b/crowdfunding/users/models.py @@ -4,4 +4,11 @@ class CustomUser(AbstractUser): pass def __str__(self): - return self.username \ No newline at end of file + """ + Return the first_name plus the last_name, with a space in between. + """ + if AbstractUser.get_full_name(self) != '': + name = AbstractUser.get_full_name(self) + else: + name = self.username + return name \ No newline at end of file