简体   繁体   English

Django rest 框架需要很长时间才能返回嵌套的序列化数据

[英]Django rest framework is taking too long to return nested serialized data

We are having four models which are related, While returning queryset serializing the data is too slow(serializer.data).我们有四个相关的模型,而返回查询集序列化数据太慢(serializer.data)。 Below are our models and serializer.下面是我们的模型和序列化器。

Why django nested serializer is taking too long to return rendered response.为什么 django 嵌套序列化程序需要很长时间才能返回呈现的响应。 What are we doing wrong here?我们在这里做错了什么?

Note:Our DB lies in AWS when connected from EC2 instance it is ok but when tried from my localhost it is insanely slow.注意:当从 EC2 实例连接时,我们的数据库位于 AWS 中,这没问题,但是当从我的本地主机尝试时,它非常慢。 And the size of json it returns is 700KB.它返回的json大小为700KB。

models.py模型.py

class ServiceType(models.Model):
    service_name = models.CharField(max_length = 100)
    description = models.TextField()
    is_active = models.BooleanField(default = 1)

class Service(models.Model):
    service_name = models.CharField(max_length = 100)
    service_type = models.ForeignKey(ServiceType, related_name = "type_of_service")
    min_duration = models.IntegerField() ##duration in mins

class StudioProfile(models.Model):

   studio_group = models.ForeignKey(StudioGroup, related_name = "studio_of_group")
   name = models.CharField(max_length = 120)

class StudioServices(models.Model):
   studio_profile = models.ForeignKey(StudioProfile, related_name = "studio_detail_for_activity")
   service = models.ForeignKey(Service, related_name = "service_in_studio")

class StudioPicture(models.Model):
   studio_profile  = models.ForeignKey(StudioProfile, related_name = "pic_of_studio")
   picture = models.ImageField(upload_to = 'img_gallery', null = True, blank = True)

serializers.py序列化程序.py

class ServiceTypeSerializer(serializers.ModelSerializer):

    class Meta:
       model = ServiceType
       fields = ('id', 'service_name')

class ServiceSerializer(serializers.ModelSerializer):

    service_type = ServiceTypeSerializer()
    class Meta:
        model = Service
        fields = ('id', 'service_type', 'service_name')

class StudioServicesSerializer(serializers.ModelSerializer):
    service = ServiceSerializer()
    class Meta:
        model = StudioServices
        fields = ('service','price','is_active','mins_takes')

class StudioPictureSerializer(serializers.ModelSerializer):
    class Meta:
        model = StudioPicture
        fields = ('picture',)

class StudioProfileSerializer(serializers.ModelSerializer):
    studio_detail_for_activity = StudioServicesSerializer(many = True)
    pic_of_studio = StudioPictureSerializer(many = True)
    class Meta:
       model = StudioProfile
       fields = ('id', 'name','studio_detail_for_activity','pic_of_studio')

views.py视图.py

class StudioProfileView(ListAPIView):
    serializer_class = StudioProfileSerializer
    model = StudioProfile
    def get_queryset(self):
        try:
           queryset = self.model.objects.all()
        except Exception ,e:
           logger_error.error(traceback.format_exc())
           return None
        else:
           return queryset

Did you checked which part is the slow one?你有没有检查哪个部分是慢的? like, How many records do you have in that db?比如,你在那个数据库中有多少条记录? and I would try to run the query and check if the query is slow, then I'd check the serializers with less than 100 registers and so on.我会尝试运行查询并检查查询是否很慢,然后我会检查少于 100 个寄存器的序列化程序等等。

I'd recommend you to read this article http://www.dabapps.com/blog/api-performance-profiling-django-rest-framework/ in order to evaluate how to profile your API我建议您阅读这篇文章http://www.dabapps.com/blog/api-performance-profiling-django-rest-framework/以评估如何分析您的 API

Regards问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM