DRF序列化器-如何返回外键字段? [英] DRF Serializer - How to return field of a foreign key?

查看:97
本文介绍了DRF序列化器-如何返回外键字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好

我真的很难解决从Django Rest Framework API返回值的问题.

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

我有两个模型,SirTarget和地位.SirTarget就像一张票&票.状态是票证的文本状态标签,与处理票证的阶段相对应.

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.

模型如下:

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)

我的序列化器如下:

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

     class Meta:
         model = SirTarget
         fields = '__all__'

字段status_text不会作为API调用的一部分返回.返回数据时,我收到状态表的PK(1、2、3等),但没有收到status_text字段.

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.

一段时间以来,我一直在苦苦挣扎.我已经引用了类似的设置,例如这篇文章:使用django-rest-framework序列化程序检索外键值

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

EDIT

我也尝试过:

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

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.0Python 3.7.1

DRF 3.9.0 Python 3.7.1

谢谢您的帮助.

BCBB

推荐答案

您应该这样做

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

这篇关于DRF序列化器-如何返回外键字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆