gpt4 book ai didi

python-3.x - 慢序列化过程 Django rest 框架

转载 作者:行者123 更新时间:2023-12-03 17:10:50 24 4
gpt4 key购买 nike

我在我的项目中使用了 django_rest_framework,但我遇到了问题。
模型.py:

from django.db import models


class RelatedField3_2(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name


class RelatedField3(models.Model):
name = models.CharField(max_length=100)
relfield3_2 = models.ForeignKey(RelatedField3_2, on_delete=models.CASCADE)
def __str__(self):
return self.name


class RelatedField2(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name


class RelatedField1(models.Model):
name = models.CharField(max_length=200)
def __str__(self):
return self.name

class MainTable(models.Model):
owner = models.ForeignKey('auth.User', on_delete=models.CASCADE)
field1 = models.IntegerField()
field2 = models.IntegerField()
field3 = models.IntegerField()
field4 = models.DecimalField(max_digits=10, decimal_places=1)
field5 = models.IntegerField(null=True)
field6 = models.IntegerField()
relfield1 = models.ForeignKey(RelatedField1, on_delete=models.CASCADE)
relfield2 = models.ForeignKey(RelatedField2, on_delete=models.CASCADE)
relfield3 = models.ForeignKey(RelatedField3, on_delete=models.CASCADE)
序列化程序.py:
from rest_framework import serializers
from main.models import MainTable, RelatedField1, RelatedField2, RelatedField3


class MainTableRelatedFields(serializers.RelatedField):
def display_value(self, instance):
return instance

def to_representation(self, value):
return str(value)

def to_internal_value(self, data):
return self.queryset.model.objects.get(name=data)


class MainTableSerializerList(serializers.ListSerializer):

def create(self, validated_data):
records = [MainTable(**item) for item in validated_data]
return self.child.Meta.model.objects.bulk_create(records)


class MainTableSerializer(serializers.ModelSerializer):
class Meta:
model = MainTable
list_serializer_class = MainTableSerializerList

id = serializers.IntegerField(write_only=False, required=False)
owner = serializers.ReadOnlyField(source='owner.username')
relfield3 = PartnerPropRelatedFields(queryset=RelatedField3.objects.all())
relfield2 = PartnerPropRelatedFields(queryset=RelatedField2.objects.all())
relfield1 = PartnerPropRelatedFields(queryset=RelatedField1.objects.all())

def create(self, validated_data):
return self.Meta.model.objects.create(**validated_data)

View .py:
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated

from .serializers import MainTableSerializer


class MainTableUploadView(viewsets.ModelViewSet):
permission_classes = (IsAuthenticated,)

@action(['post'], detail=False)
def upload_records(self, request, *args, **kwargs):
seriazlizer = MainTableSerializer(data=request.data, many=True)
seriazlizer.is_valid(raise_exception=True)
seriazlizer.save(owner=request.user)
return Response(seriazlizer.data)
发送_json.py:
import requests

data_load_list = [{
"field1": 1,
"field2": 2,
"field3": 3,
"field4": "field4",
"field5": 5,
"field6": "field6",
"relfield1": "test1",
"relfield2": "test2",
"relfield3" : "test3"
} for i in range(1000)
]

load_list_response = requests.post(url=url_upload, headers={'Authorization': f'Token {TOKEN}'},
json=data_load_list)
当我开始 send_json.py 发送时 1000 记录,我只是收到错误:
RemoteDisconnected                        Traceback (most recent call last)
~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
669 # Make the request on the httplib connection object.
--> 670 httplib_response = self._make_request(
671 conn,

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
425 # Otherwise it looks like a bug in the code.
--> 426 six.raise_from(e, None)
427 except (SocketTimeout, BaseSSLError, SocketError) as e:

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/urllib3/packages/six.py in raise_from(value, from_value)

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
420 try:
--> 421 httplib_response = conn.getresponse()
422 except BaseException as e:

/usr/lib/python3.8/http/client.py in getresponse(self)
1331 try:
-> 1332 response.begin()
1333 except ConnectionError:

/usr/lib/python3.8/http/client.py in begin(self)
302 while True:
--> 303 version, status, reason = self._read_status()
304 if status != CONTINUE:

/usr/lib/python3.8/http/client.py in _read_status(self)
271 # sending a valid response.
--> 272 raise RemoteDisconnected("Remote end closed connection without"
273 " response")

RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:

ProtocolError Traceback (most recent call last)
~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
438 if not chunked:
--> 439 resp = conn.urlopen(
440 method=request.method,

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
725
--> 726 retries = retries.increment(
727 method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/urllib3/util/retry.py in increment(self, method, url, response, error, _pool, _stacktrace)
402 if read is False or not self._is_method_retryable(method):
--> 403 raise six.reraise(type(error), error, _stacktrace)
404 elif read is not None:

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/urllib3/packages/six.py in reraise(tp, value, tb)
733 if value.__traceback__ is not tb:
--> 734 raise value.with_traceback(tb)
735 raise value

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
669 # Make the request on the httplib connection object.
--> 670 httplib_response = self._make_request(
671 conn,

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
425 # Otherwise it looks like a bug in the code.
--> 426 six.raise_from(e, None)
427 except (SocketTimeout, BaseSSLError, SocketError) as e:

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/urllib3/packages/six.py in raise_from(value, from_value)

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
420 try:
--> 421 httplib_response = conn.getresponse()
422 except BaseException as e:

/usr/lib/python3.8/http/client.py in getresponse(self)
1331 try:
-> 1332 response.begin()
1333 except ConnectionError:

/usr/lib/python3.8/http/client.py in begin(self)
302 while True:
--> 303 version, status, reason = self._read_status()
304 if status != CONTINUE:

/usr/lib/python3.8/http/client.py in _read_status(self)
271 # sending a valid response.
--> 272 raise RemoteDisconnected("Remote end closed connection without"
273 " response")

ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

During handling of the above exception, another exception occurred:

ConnectionError Traceback (most recent call last)
<timed exec> in <module>

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/requests/api.py in post(url, data, json, **kwargs)
117 """
118
--> 119 return request('post', url, data=data, json=json, **kwargs)
120
121

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/requests/api.py in request(method, url, **kwargs)
59 # cases, and look like a memory leak in others.
60 with sessions.Session() as session:
---> 61 return session.request(method=method, url=url, **kwargs)
62
63

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/requests/sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
528 }
529 send_kwargs.update(settings)
--> 530 resp = self.send(prep, **send_kwargs)
531
532 return resp

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/requests/sessions.py in send(self, request, **kwargs)
641
642 # Send the request
--> 643 r = adapter.send(request, **kwargs)
644
645 # Total elapsed time of the request (approximately)

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
496
497 except (ProtocolError, socket.error) as err:
--> 498 raise ConnectionError(err, request=request)
499
500 except MaxRetryError as e:

ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
当我开始 send_json.py 发送时 100 记录,数据被加载到数据库中并返回代码 200。这需要 15 秒。
我如何以及用什么来优化我的代码,以便我可以将 1000 多条记录发送到数据库并快速获取响应正文中的信息?

最佳答案

帮助我的解决方案(请参阅拉取请求中的更改):
https://github.com/hax2000/Relaed-Fields-API/pull/1/files

关于python-3.x - 慢序列化过程 Django rest 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63723665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com