I measured how many seconds it takes to prepare the data for the request, 4 seconds came out. In the browser, the request is displayed only after 20 seconds. What could this be related to?
Product table has about 100,000 records. Database queries are executed in 0.5 seconds
views.py
class ProductImproveView(ModelViewSet):
filterset_class = ProductFilter
serializer_class = ProductListingSerializerLightImproved
def get_queryset(self):
return {
'qs': Product.objects.all()[:30],
'count': Product.objects.all().count()
}
def list(self, request, processed_queryset=None, *args, **kwargs):
start_time = datetime.now()
result = self.get_queryset()
queryset = result['qs']
count = result['count']
serializer = self.get_serializer(queryset, many=True, context={'request': request})
d = serializer.data
g = JsonResponse({
'count': count,
'next': 'nd',
'results': d
})
print(g.content)
print(datetime.now() - start_time) # 4 seconds
return g
urls.py
router = DefaultRouter()
router.register(r'products-improve', ProductImproveView, basename='products')
urlpatterns = []
urlpatterns += router.urls
serializer.py
class ProductListingSerializerLightImproved(serializers.ModelSerializer):
store_title = serializers.SerializerMethodField()
category = serializers.SerializerMethodField()
store_logo = serializers.SerializerMethodField()
rating = serializers.SerializerMethodField()
is_new = serializers.SerializerMethodField()
image = serializers.SerializerMethodField()
stock = serializers.SerializerMethodField()
price = serializers.SerializerMethodField()
class Meta:
model = Product
fields = ['id', 'title', 'slug', 'is_new', 'discount', 'rating', 'rating', 'store_title', 'store_logo', \
'category', 'image', 'stock', 'price']
def get_price(self, obj):
return PriceSerializer(obj.variations.order_by('order')[:1][0]).data
def get_store_title(self, obj):
return obj.store.title
def get_store_logo(self, obj):
return "{}{}".format('https://pasimol.s3.amazonaws.com/', obj.store.logo)
def get_category(self, obj):
return CategorySerializerLight(obj.category, many=True).data
def get_image(self, obj):
return S3ImageSerializerLight(obj.variations.all()[:1][0].images.all(), many=True).data
def get_rating(self, obj):
return decimal.Decimal(random.randrange(0, 50)) / 10
def get_is_new(self, obj):
new_delta = datetime.timedelta(days=1)
delta = timezone.now() - obj.created_at
if delta > new_delta:
return False
return True
def get_stock(self, obj):
return obj.stock