0

我很难连接关于如何创建允许外键的视图和序列化程序的 django 和 django rest 框架上的所有文档。

编辑:我可能在这里有一个答案:http: //www.django-rest-framework.org/api-guide/relations/#writable-nested-serializers

示例我有这些模型。

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)

我希望能够选择一个城市,然后查看该城市拥有的所有社区,并能够添加社区、编辑社区和删除社区。

所以也许是获取城市所有社区或为城市创建新社区的 url

url(r'^neighborhood/(?P<citypk>[0-9]+)$', SearchNeighborhoodListCreate.as_view()),

和一个编辑和删除邻域:

url(r'^neighborhood/(?P<citypk>[0-9]+)/(?P<neighborhoodpk>[0-9]+)$',SearchNeighborhoodDetail.as_view()),

我目前正在使用DRF Generics的ListCreateAPIViewRetreiveUpdateDestoryAPIView

我知道我们有这样的选择query_setrelated,可以让我们获得模型所具有的所有关系。

我知道我们可以x_set选择。在我的例子中像这样使用。Searchcity.SearchNeighborhood_set.all()

我知道我们有相关的序列化程序

我创建它们的正确方法是这样的:

class CityNeighborhoodSerializer(serializers.ModelSerializer):
    neighborhood = serializers.PrimaryKeyRelatedField(many=True, read_only=False)

    class Meta:
        model = SearchCity
        fields = ('City', 'neighborhood')

但是在这个用例中我该如何使用它呢?

http://www.django-rest-framework.org/api-guide/relations/#serializer-relations

关于获取关系中的所有对象有一个很好的参考链接在这里 http://gregblogs.com/tlt-how-to-retrieve-the-fields-of-a-related-model-from-a-django-休息框架端点/

但这适用于编辑、删除相关对象吗?

最终我做了很多研究,但我正在寻求帮助以填补裂缝并真正理解这件事。这是一个常见的用例,我相信你们中的许多人以前都做过。

编辑:

看起来这个问题间接地回答了我的问题,但我仍然不确定。我将继续研究并测试它。看看我发现了什么。

删除用作外键的对象

4

2 回答 2

1

以下是我对此的看法:

网址:

url(r'^city/(?P<city_pk>[0-9]+)/neighborhood/', SearchNeighborhoodListCreateView.as_view()),
url(r'^city/(?P<city_pk>[0-9]+)/neighborhood/(?P<pk>[0-9]+)$', SearchNeighborhoodDetailView.as_view()),

因为每个城市都有一个或多个街区

序列化器:

  • 只需为此创建一个序列化程序SearchNeighborhood

    class NeighborhoodSerializer(serializers.ModelSerializer):
    
        class Meta:
            model = SearchNeighborhood
            fields = ('id', 'city', 'neighborhood')
    
  • 如果你想要你的城市 api 中的社区列表,你可以使用这个:

    class CityNeighborhoodSerializer(serializers.ModelSerializer):
        neighborhoods = NeighborhoodSerializer(many=True, source='searchneighborhood_set.all')
    
        class Meta:
            model = SearchCity
            fields = ('city', 'neighborhoods')
    

我建议SearchNeighborhood在字段中的模型上使用相关名称city,例如:related_name='neighborhoods',然后您可以使用source='neighborhoods.all',它更具可读性。

视图: 这里的诀窍是只获取与城市相关的社区

class SearchNeighborhoodListCreateView(generics.ListCreateAPIView):
    queryset = SearchNeighborhood.objects.all()
    serializer_class = NeighborhoodSerializer

    def get_city(self):
        queryset = SearchCity.objects.all()
        return get_object_or_404(queryset, pk=self.kwargs['city_pk'])

    def get_queryset(self):
        city = self.get_city()
        return super().get_queryset().filter(city=city)

    def perform_create(self, serializer):
        city = self.get_city()
        serializer.save(city=city)

希望你能得到主要的想法。

于 2018-03-03T07:17:06.820 回答
0

在这里,我再次回答我自己的问题......

最好的方法是创建我自己的视图来按城市创建和获取对象。

# create and get list of Search Neighborhood objects by city
class CityNeighborhoodsListCreate(APIView):

    # this get request gets all the neighborhoods by city
    def get (self, request, format=None, *args, **kwargs):
        citypk = kwargs.get('citypk', None)
        city = get_object_or_404(SearchCity,pk=citypk)
        neighborhoods = city.searchneighborhood_set.all()
        serialized = SearchNeighborhoodSerializer(neighborhoods, many=True)
        return Response({
            'neighborhoods': serialized.data
        })

    def post(self, request, *args, **kwargs):
        citypk = kwargs.get('citypk', None)
        city=get_object_or_404(SearchCity,pk=citypk)
        serialized = SearchNeighborhoodSerializer(data=request.data)
        if serialized.is_valid(raise_exception=True):
            validatedData = serialized.validated_data
            neighborhood = validatedData.get('neighborhood')
            neighborhoodobject = SearchNeighborhood(neighborhood= neighborhood, city = city)
            neighborhoodobject.save()
            createdneighborhood = SearchNeighborhoodSerializer(neighborhoodobject)
            return Response({
                'neighborhood': createdneighborhood.data
            })
于 2018-03-03T07:03:15.590 回答