4

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

Bring Coffee Bring Beer
  • 1,035
  • 3
  • 11
  • 19

2 Answers2

5

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
Nafees Anwar
  • 6,324
  • 2
  • 23
  • 42
  • OK, I figured it out. ReadOnlyField wasn't working and I messed up some OTHER things while trying to figure it out. Once I got those changes backed off. I tried the CharField as suggested and its working as expected. Thank you! – Bring Coffee Bring Beer Apr 29 '19 at 14:34
3

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

class SirTargetStatusSerializer(serializers.ModelSerializer):

     class Meta:
         model = SirTarget
         fields = ('__all__')
         depth = 1
niekas
  • 8,187
  • 7
  • 40
  • 58
SEJ
  • 296
  • 1
  • 16
  • Thanks for this! A much simpler method, given that you can then consume the FK fields as needed, rather than specifying every field. – cryocaustik Jan 20 '20 at 09:33