Skip to content

Commit

Permalink
pagination for api responses wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Amey Tendulkar committed Oct 30, 2023
1 parent 008c6c5 commit e6e122a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
globalmart/__pycache__/
globalmart/__pycache__/
logs/
6 changes: 3 additions & 3 deletions api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ class SubCategorySerializer(serializers.ModelSerializer):
class Meta:
model = SubCategory
fields = '__all__'

class ProductSerializer(serializers.ModelSerializer):
sub_category = serializers.SerializerMethodField()
'''sub_category = serializers.SerializerMethodField()
def get_sub_category(self, product):
subcategory = SubCategorySerializer(product.sub_category)
return subcategory.data
return subcategory.data'''
class Meta:
model = Product
fields = '__all__'
2 changes: 1 addition & 1 deletion api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
from django.urls import path

urlpatterns = [
path('products-by-category/<int:id>', ProductsByCategory.as_view()),
path('products-by-category/', ProductsByCategory.as_view()),

]
16 changes: 12 additions & 4 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@
from rest_framework import status as api_response_status
from api.models import *
from api.serializers import *
from django.core.paginator import Paginator

class ProductsByCategory(APIView):

def get(self, request, id, format=None):
def get(self, request, format=None):

category = Category.objects.get(id=id)
subcategories = SubCategory.objects.filter(category=category)
category_id = request.GET.get('category')
page = request.GET.get('page')

category = Category.objects.get(id=category_id)
subcategories = SubCategory.objects.filter(category=category)
products = Product.objects.filter(sub_category__in=subcategories)

products = ProductSerializer(products, many=True)
paginator = Paginator(products, 10)
products_page = paginator.get_page(page)


products = ProductSerializer(products_page, many=True)

return JsonResponse({
'success': True,
Expand Down
30 changes: 30 additions & 0 deletions globalmart/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,36 @@
},
]

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '{levelname} {asctime} {module} {process:d} {thread:d} {message}',
'style': '{',
},
'simple': {
'format': '{levelname} {asctime} {module} {message}',
'style': '{',
}
},
'handlers': {
'file2': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': 'logs/sqlquery.log',
'formatter': 'simple'
},
},
'loggers': {
'django.db.backends': {
'handlers': ['file2'],
'level': 'DEBUG',
'propagate': True,
},
}
}

WSGI_APPLICATION = "globalmart.wsgi.application"


Expand Down

0 comments on commit e6e122a

Please sign in to comment.