Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DRF Serializer - How to return field of a foreign key?

Good morning,

I am really struggling with an issue returning a value from my Django Rest Framework API.

I have two models, SirTarget & Status. The SirTarget is like a ticket & the Status is a textual status label of the ticket that corresponds to the phase of handling the ticket.

The models are as follows:

class Status(models.Model):
     status_text = models.CharField(max_length=20)
     status_open = models.BooleanField(default=1)

     def __str__(self):
        return self.status_text


 class SirTarget(models.Model):
     name = models.CharField(max_length=70)
     entry_date = models.DateTimeField(auto_now_add=True)
     last_name = models.CharField(max_length=40)
     first_name = models.CharField(max_length=40)
     sir_status = models.ForeignKey(Status, on_delete=models.CASCADE, default=1, related_name='targets')

     def __str__(self):
        return '%d - %s %s' % (self.id, self.first_name, self.last_name)

My serializer looks like this:

 class SirTargetStatusSerializer(serializers.ModelSerializer):
     status_text = serializers.ReadOnlyField(source='Status.status_text')

     class Meta:
         model = SirTarget
         fields = '__all__'

The field status_text is not coming back as part of the API call. When I return the data, I receive the PK of the Status table (1,2,3, etc.) but I do not receive the status_text field.

I have been messing around with this for a while and struggling. I have referenced similar set ups such as in this post: Retrieving a Foreign Key value with django-rest-framework serializers

However, nothing seems to be working for me.

EDIT

I have also tried:

status_text = serializers.RelatedField(source='sir_status.status_text', read_only=True)

and

status_text = serializers.CharField(source='sir_status.status_text', read_only=True)

When I look directly in the DB, I see what I am looking for and verified that the values are populated as expected:

# select * from sir_admin_status;
 id |  status_text   | status_open 
----+----------------+-------------
  1 | New            | t
  2 | Open           | t
  3 | Referred       | f
  4 | Resolved       | f
  5 | False Positive | f

DRF 3.9.0 Python 3.7.1

Thank you for your help.

BCBB

like image 309
Bring Coffee Bring Beer Avatar asked Jan 26 '23 09:01

Bring Coffee Bring Beer


2 Answers

You should do

class SirTargetStatusSerializer(serializers.ModelSerializer):
     status_text = serializers.CharField(source='sir_status.status_text', read_only=True)

     class Meta:
         model = SirTarget
         fields = ('name', ... , 'status_text') # explicitly define all field you want here
like image 115
Nafees Anwar Avatar answered Jan 30 '23 05:01

Nafees Anwar


To access all the fields in the foreign key table use "depth"

class SirTargetStatusSerializer(serializers.ModelSerializer):

     class Meta:
         model = SirTarget
         fields = ('__all__')
         depth = 1
like image 20
SEJ Avatar answered Jan 30 '23 05:01

SEJ