Django Rest框架JSONAPI中的大容量插入

2024-10-03 02:46:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在实现一个DRFJSonAPIhttps://django-rest-framework-json-api.readthedocs.io/en/stable/index.html

然而,我在尝试实现大容量插入时遇到了困难,似乎JSONAPI specification没有提到任何内容 我发现这只是一个实验性的特性:https://springbot.github.io/json-api/extensions/bulk/

而且它似乎不是由DRFJSONAPI实现的

我尝试了几种方法,从在我的视图集中定义一个新的@action的基本方法,到仅仅添加DRFAPI_视图并从那里开始。 但是看起来我的序列化程序没有正确理解数据,返回了一个空字典

我的序列化程序:


from rest_framework_json_api import serializers, relations, views

class VideoScriptRowSerializer(serializers.ModelSerializer):

    included_serializers = {
        'video': 'videos.serializers.VideoOnDemandSerializer',
    }

    class Meta:
        model = VideoScriptRow
        fields = (
            'video_id',
            'index',
            'character',
            'guessed',
            'is_locked',
            'loop',
            'text',
            'time_start',
        )


class VideoOnDemandSerializer(serializers.ModelSerializer):
    script_rows = relations.ResourceRelatedField(
        queryset=VideoScriptRow.objects,  # queryset argument is required
        required=False,
        many=True,
    )
    included_serializers = {
        'script_rows': 'videos.serializers.VideoScriptRowSerializer',
    }

    class Meta:
        model = VideoOnDemand
        fields = (
            'title',
            'workflow_status',
            'initial_offset',
            'video_offset',
            'video_input',
            'thumbnail_url',
            'transcript_uri',
            'created_at',
            'script_rows',
        )
        read_only_fields = ('created_at', 'workflow_status')

我的看法:


    from rest_framework_json_api.views import ModelViewSet, RelationshipView

    class VideoScriptRowViewSet(ModelViewSet):
        queryset = VideoScriptRow.objects.all()
        serializer_class = VideoScriptRowSerializer


    class VideoOnDemandViewSet(ModelViewSet):
        permission_classes = [IsAuthenticated]
        queryset = VideoOnDemand.objects.all()
        serializer_class = VideoOnDemandSerializer

基于DRF文档,您似乎只需要将many=True传递给序列化程序实例就可以进行批量创建,但我找到的所有示例都是关于序列化以显示对象列表,而不是关于反序列化来自POST的数据

我还尝试直接从ListSerializer继承,并仅为此操作创建视图集。。。当我使用项目列表时,请求中仍然没有任何数据

我上一次尝试只是简单地添加了一个@api_视图装饰器,因为我认为它对drfjsonapi免疫,但在我的request.data上仍然没有

api_视图实现:

@api_view(['post'])
def update_script_rows(request):
    serializer = VideoScriptRowSerializer(data=request.data, many=True)  # request.data is empty here
    serializer.is_valid(raise_exception=True)
    # Here goes the bulk insertion
    return Response(status=status.HTTP_201_CREATED)

调用视图的测试实现:

    def test_update_script_rows(self):
        self.client.login(email='foobar@invalid.com', password='secretpassword')
        url = reverse('update-script-rows')

        response = self.client.post(
            url,
            {
                'data': [{
                    'type': 'VideoScriptRow',
                    'attributes': {
                        'index': 1,
                        'video_id': self.v1.id,
                        'character': 'johndoe',
                        'text': 'hello, world2',
                    }
                }],
            },
            format='vnd.api+json'
        )

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

如果此操作在VideoScriptRowViewSet上,当它是单个对象时,我可以在request.data中看到数据,但当它是列表时,我什么也看不到。 如果我在@api\u视图上执行此操作,我会得到一个错误:rest\u framework\u json\u api.exceptions.Conflict:The resource object's type (VideoScriptRow) is not the type that constitute the collection represented by the endpoint (UpdateScriptRows). 即使在将序列化程序的基类从DRFJSONAPI更改为DRF之后,在其他地方也没有看到指向DRFJSONAPI的链接

你知道如何在DRFJSONAPi上轻松实现批量创建吗?我现在最好的想法是摆脱JSONAPI,在普通DRF上做所有事情,但如果可能的话,我希望保留JSONAPI结构


Tags: self视图apijsondata序列化isrequest