Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

280
Views
Name returning a number after serializing data

I have a table service :

from django.db import models
from users.models import CustomUser

SERVICE_CHOICES = (
    ('Carpenter', 'Carpenter'),
    ('Driver', 'Driver'),
    ('Ambulanve', 'Ambulanve'),
    ('Spa', 'Spa'),
    ('Barber', 'Barber'),
    ('Cleaning', 'Cleaning'),
    ('Cook', 'Cook'),
)



class Service(models.Model):
    name  = models.ForeignKey(CustomUser, on_delete=models.CASCADE, limit_choices_to={'is_worker': True},)

    service = models.CharField(choices=SERVICE_CHOICES,max_length=100)

    def __str__(self):
        return f'{self.service} - {self.name}'

and a table CustomUser :

from django.db import models
from django.contrib.auth.models import AbstractUser

class CustomUser(AbstractUser):
    is_worker = models.BooleanField(default=False)
    is_customer = models.BooleanField(default=True)

I am serializing the Service table below :

from rest_framework import serializers

from django.contrib.auth.models import User
from .models import *

class ServiceSerializer(serializers.ModelSerializer):
    class Meta:
        model = Service
        fields = '__all__'

But when I add a service from the admin panel, the name shows a number in the browser and not the string. How do I change it to show a the name and not a number? enter image description here

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

In your ServiceSerializer you can try:

class ServiceSerializer(serializers.ModelSerializer):
    class Meta:
        model = Service
        fields = '__all__'

    def to_representation(self, instance):
        rep = super(ServiceSerializer, self).to_representation(instance)
        rep['name'] = instance.name.name    # instance.name.what_you_use_in_Custom_User_model_to_represent_name
        return rep

However, consider whether this is what you want, because this change could lead to more problems than it solves.

over 4 years ago · Santiago Trujillo Report

0

This is because name is a forignkey to CustomUser, and the default behavior is to return the PK related to the CustomUser instance.

Instead you could use SerializerMethodField.

class UserSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()

    class Meta:
        model = Service
        fields = '__all__'

    def get_name(self, obj):
        return obj.name.first_name
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!