如何:创建用于添加、编辑和删除具有外部关系的对象的视图和序列化程序 django rest framework
how to: creating a view and serializer for adding, editing, and deleting objects with foreign relationships django rest framework
我很难连接所有关于 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
的 ListCreateAPIView
和 RetreiveUpdateDestoryAPIView
我知道我们有像 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
关于获取所有对象的关系有很好的参考 link 在这里
http://gregblogs.com/tlt-how-to-retrieve-the-fields-of-a-related-model-from-a-django-rest-framework-endpoint/
但这是否适用于编辑、删除相关对象?
最终我做了很多研究,但我正在寻求帮助来填补裂缝并真正理解这件事。这是一个常见的用例,我相信你们中的许多人以前都做过。
编辑:
看起来这个问题间接回答了我的问题,但我仍然不确定。我将继续关注并测试它。看看我发现了什么。
Deletion objects that is used as foreign key
我又来回答我自己的问题了...
最好的方法是创建我自己的视图以按城市创建和获取对象。
# 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
})
以下是我对此的看法:
网址:
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')
我建议在city
字段中使用与SearchNeighborhood
模型相关的名称,例如: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)
希望你明白主要思想。
我很难连接所有关于 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
的ListCreateAPIView
和 RetreiveUpdateDestoryAPIView
我知道我们有像 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
关于获取所有对象的关系有很好的参考 link 在这里 http://gregblogs.com/tlt-how-to-retrieve-the-fields-of-a-related-model-from-a-django-rest-framework-endpoint/
但这是否适用于编辑、删除相关对象?
最终我做了很多研究,但我正在寻求帮助来填补裂缝并真正理解这件事。这是一个常见的用例,我相信你们中的许多人以前都做过。
编辑:
看起来这个问题间接回答了我的问题,但我仍然不确定。我将继续关注并测试它。看看我发现了什么。
Deletion objects that is used as foreign key
我又来回答我自己的问题了...
最好的方法是创建我自己的视图以按城市创建和获取对象。
# 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
})
以下是我对此的看法:
网址:
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')
我建议在city
字段中使用与SearchNeighborhood
模型相关的名称,例如: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)
希望你明白主要思想。