How to access foreign key field from another model in serializer

I'm trying to access some fields from another model in my serializer but I can't get it working by any means. Here is how I'm going about this. Here are my models:

class Category(models.Model):
    name = models.CharField(max_length=256)

    class Meta:
        ordering = ('name',)

    def __str__(self):
        return self.name


class Contact(models.Model):
    first_name = models.CharField(max_length=256)
    last_name = models.CharField(max_length=256)
    address = models.CharField(max_length=1024)
    city = models.CharField(max_length=256)
    country = models.ForeignKey(Country, on_delete=models.CASCADE)
    email = models.EmailField(max_length=256, unique=True)
    phone = models.CharField(max_length=256)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    integration = models.ForeignKey(Integration, null=True, on_delete=models.SET_NULL)

    def __str__(self):
        return self.first_name


class Company(models.Model):
    name = models.CharField(max_length=256)
    address = models.CharField(max_length=256)
    city = models.CharField(max_length=256)
    country = models.ForeignKey(Country, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.name


class ContactCompany(models.Model):
    contact = models.ForeignKey(Contact, on_delete=models.CASCADE, related_name='job')
    company = models.ForeignKey(Company, on_delete=models.CASCADE, related_name='company')
    job = models.TextField(blank=True, help_text='Job', max_length=5000, null=True)
    started_at = models.DateTimeField(auto_now_add=True)
    finished_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.job

And here is my serializer

class ContactSerializer(serializers.ModelSerializer):

    country_name = serializers.CharField(source='country.name')
    category_name = serializers.CharField(source='category.name')
    user_name = serializers.CharField(source='user.email')
    integration_name = serializers.CharField(source='integration.name')

    class Meta:
        model = Contact
        fields = ('first_name', 'last_name', 'address', 'city', 'country_name', 'email', 'phone',
                  'category_name', 'user_name', 'integration_name', 'job', )

How can I access the company field from the ContactCompany model in this serializer?

Here is how the API response looks like

{
            "first_name": "First name",
            "last_name": "Last name",
            "address": "Address",
            "city": "City",
            "country_name": "Gabon",
            "email": "saidsadiasida@gmam.com",
            "phone": "0712345678",
            "category_name": "TestCategory",
            "user_name": "ekartdragos@cyberaxo.com",
            "integration_name": "testmonth",
            "job": [
                4
            ]
        }

How can I get the job text be desplayed instead of the id?

1 answer

  • answered 2019-11-08 14:53 Shakil

    One possible solution is to used serializerMethodField and get your desire information.

    class ContactSerializer(serializers.ModelSerializer):
    
        country_name = serializers.CharField(source='country.name')
        category_name = serializers.CharField(source='category.name')
        user_name = serializers.CharField(source='user.email')
        integration_name = serializers.CharField(source='integration.name')
        company = serializers.SerializerMethodField()
    
        class Meta:
            model = Contact
            fields = ('first_name', 'last_name', 'address', 'city', 'country_name', 'email', 'phone',
                      'category_name', 'user_name', 'integration_name', 'job', 'company' )
        def get_company(self, obj): 
        """
        at first we get the instance of ContactCompany from the id of object. then get the company information 
        """
            try:
                contact_company = ContactCompany.objects.get(contact=obj.id)
            expect ContactCompany.DoesNotExist: 
                # return or raise your desire this 
    
            return contact_company.company # here we have company id of this contact, also we can generate all company information from this.