如何获取与主键相关的字段的所有值的嵌套序列化器Django Rest Framework [英] how to get all values of primary key related field nested serializer django rest framework

查看:148
本文介绍了如何获取与主键相关的字段的所有值的嵌套序列化器Django Rest Framework的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下型号:

class SearchCity(models.Model):
    city = models.CharField(max_length=200)

class SearchNeighborhood(models.Model):
    city = models.ForeignKey(SearchCity, on_delete=models.CASCADE)
    neighborhood = models.CharField(max_length=200)

,然后是以下嵌套的序列化器:

and then the following nested serializer:

class CityNeighborhoodReadOnlySerializer(serializers.ModelSerializer):
    searchneighborhood_set = serializers.PrimaryKeyRelatedField(many=True, read_only=True)

    class Meta:
        model = SearchCity
        fields = ('city','searchneighborhood_set')
        read_only_fields =('city', 'searchneighborhood_set')

与视图配对:

class CityNeighborhoodView(ListAPIView):
    queryset = SearchCity.objects.all()
    serializer_class = CityNeighborhoodReadOnlySerializer

当我进行api调用时,我得到了:

when I make the api call I get this:

city: "Chicago"
    searchneighborhood_set: 
      0: 5 
      1: 4
      2: 3
city: "New York"
    searchneighborhood_set:
      0: 8
      1: 7
      2: 6

我只是获得相关对象的主键.我需要那个很好,但是我还想要neighborhood这个名字怎么得到的?

Im just getting the primary keys of the objects related. Which is good I need that, but I also want the neighborhood name how do I get that?

这个问题可能会有所帮助.他们没有使用与主键相关的序列化程序,所以我的问题是(如果这当然可行,那么与主键相关的序列化程序的意义是什么?

This question may shead some light. They are not using the primary key related serializer though, so my question would be (if this works of course, is what is the point of the primarykey related serializer then?

Django Rest Framework嵌套的序列化程序未显示相关数据

推荐答案

答案是不使用primarykeyrelatedserializer,而是使用用于序列化Searchneighborhood对象的序列化程序.

The answer was to not use the primarykeyrelatedserializer but rather the serializer used to serialize Searchneighborhood objects.

我更改了此内容:

class CityNeighborhoodReadOnlySerializer(serializers.ModelSerializer):
    searchneighborhood_set = serializers.PrimaryKeyRelatedField(many=True, read_only=True)

    class Meta:
        model = SearchCity
        fields = ('city','searchneighborhood_set')
        read_only_fields =('city', 'searchneighborhood_set')

对此:

class CityNeighborhoodReadOnlySerializer(serializers.ModelSerializer):
    searchneighborhood_set = SearchNeighborhoodSerializer(many=True, read_only=True)

    class Meta:
        model = SearchCity
        fields = ('city','searchneighborhood_set')
        read_only_fields =('city', 'searchneighborhood_set')

并从以下输出中移出:

city: "Chicago"
    searchneighborhood_set: 
      0: 5 
      1: 4
      2: 3
city: "New York"
    searchneighborhood_set:
      0: 8
      1: 7
      2: 6

对我想要的人:

city: Chicago
    searchneighborhood_set:
         0: {pk: 5, neighborhood: 'River North}
    ....

但是现在出现了一个新问题,主键实现序列化器的意义是什么?

but now a new question arises, what is the point of a primary key realated serializer?

这篇关于如何获取与主键相关的字段的所有值的嵌套序列化器Django Rest Framework的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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