【问题标题】:Explicitly fields aren't returned in serializer in django-rest-framework在 django-rest-framework 的序列化程序中不显式返回字段
【发布时间】:2021-01-13 14:36:15
【问题描述】:

我正在尝试获取模型的序列化程序,该模型具有来自另一个模型的字段,这些字段由外键关系关联。期望的结果应该是:

"coordinates": [
        {
            "coordinate": "42.0677",
            "coordinate_type": "shooting_latitude"
        },
        {
            "coordinate": "-8.0990",
            "coordinate_type": "shooting_longitude"
        }
]

模型是:

class CoordinatesType(models.Model):
    coordinate_type = models.CharField(max_length=100, null=False, blank=False)
    coordinate_description = models.TextField(null=False, blank=False)

class Coordinates(models.Model):
    coordinate_type = models.ForeignKey('hauls.CoordinatesType', null=False, blank=False, on_delete=models.CASCADE)
    coordinate = models.DecimalField(max_digits=10, decimal_places=4, null=False, blank=False)

我可以用嵌套的序列化器来做到这一点:

class CoordinatesTypeSerializer(serializers.ModelSerializer):
    class Meta:
        model = CoordinatesType
        fields = ['coordinate_type']

class CoordinatesSerializer(serializers.ModelSerializer):
    coordinate_type = CoordinatesTypeSerializer(read_only=True)

    class Meta:
        model = Coordinates
        fields = ['coordinate', 'coordinate_type', ]

在这种情况下,序列化器的结果是:

"coordinates": [
        {
            "coordinate": "42.0677",
            "coordinate_type": {
                "coordinate_type": "shooting_latitude"
            }
        },
        {
            "coordinate": "-8.0990",
            "coordinate_type": {
                "coordinate_type": "shooting_longitude"
            }
        }
]

然后我可以重写 to_representation() 方法以避免嵌套数据并获得所需的结果。但是,在这种只有一个嵌套字段的情况下,我认为创建一个显式字段可能更简单,我尝试了:

class CoordinatesSerializer(serializers.ModelSerializer):
    coordinate_type = serializers.CharField(read_only=True, source="coordinatestype.coordinate_type")

    class Meta:
        model = Coordinates
        fields = ['coordinate', 'coordinate_type', ]

但看起来序列化程序忽略了返回此的显式字段:

"coordinates": [
        {
            "coordinate": "42.0677"
        },
        {
            "coordinate": "-8.0990"
        }
]

为什么会出现这个结果?我认为我缺少一些东西。

【问题讨论】:

  • 我相信你的序列化器字段中的source="coordinate_type.coordinate_type" 应该可以解决它?
  • 谢谢,它有效...但我不明白为什么第一个 'coordinate_type' 是 'coordinate_type',而不是 'coordinatestype'。
  • 如果您不介意验证它,我给出了更详细的答案。如果这还不够解释,请不要犹豫。

标签: django serialization django-rest-framework


【解决方案1】:

从我的评论来看,答案是:

class CoordinatesSerializer(serializers.ModelSerializer):
    coordinate_type = serializers.CharField(read_only=True, source="coordinate_type.coordinate_type")

    class Meta:
        model = Coordinates
        fields = ['coordinate', 'coordinate_type', ]

解释:

您的序列化程序模型是 CoordinatesType,外键关系由另一个模型 Coordinates 上的字段 coordinate_type 表示。这是你试图做的反向关系。

或者,如果您想在序列化程序中保留source="coordinatestype.coordinate_type",则需要更改模型中的related_name 参数,如下所示:

class Coordinates(models.Model):
    coordinate_type = models.ForeignKey('hauls.CoordinatesType', 
                                        null=False, blank=False, 
                                        on_delete=models.CASCADE, 
                                        related_name='coordinatestype')

希望能给你更多的解释。

【讨论】:

    【解决方案2】:

    Django 以不同方式跨关系字段进行查询。

    source = "coordinate_type__coordinate_type" 应该适用于您的情况。

    注意字段名和相关模型的字段名之间的双_

    【讨论】:

      猜你喜欢
      • 2023-02-08
      • 2019-01-17
      • 1970-01-01
      • 2013-11-15
      • 1970-01-01
      • 2016-04-10
      • 1970-01-01
      • 1970-01-01
      • 2013-07-07
      相关资源
      最近更新 更多