Django Rest Framework pagination and filtering - django

Hi guys i am trying to make filtering with pagination but i cannot get the result i want.
This is my function in views.py.
class OyunlarList(generics.ListAPIView):
# queryset = Oyunlar.objects.all()
pagination_class = StandardPagesPagination
filter_backends = [DjangoFilterBackend]
filterset_fields = ['categories__name', 'platform']
# serializer_class = OyunlarSerializer
def get_queryset(self):
queryset=Oyunlar.objects.all()
oyunlar=OyunlarSerializer.setup_eager_loading(queryset)
return oyunlar
def get(self,request,*args,**kwargs):
queryset = self.get_queryset()
serializer=OyunlarSerializer(queryset,many=True)
page=self.paginate_queryset(serializer.data)
return self.get_paginated_response(page)
This is my pagination class.
class StandardPagesPagination(PageNumberPagination):
page_size = 10
And this is the json i got but when i write localhost/api/games?platform=pc or localhost/api/games?categories=Action it is not working.
{
"count": 18105,
"next": "http://127.0.0.1:8000/api/oyunlar?categories__name=&page=2&platform=pc",
"previous": null,
"results": [
{
"game_id": 3,
"title": "The Savior's Gang",
"platform": "ps4",
"image": "https://store.playstation.com/store/api/chihiro/00_09_000/container/TR/en/999/EP3729-CUSA23817_00-THESAVIORSGANG00/1599234859000/image?w=240&h=240&bg_color=000000&opacity=100&_version=00_09_000",
"categories": [],
"release_date": null
},
{
"game_id": 8,
"title": "Spellbreak",
"platform": "ps4",
"image": "https://store.playstation.com/store/api/chihiro/00_09_000/container/TR/en/999/EP0795-CUSA18527_00-SPELLBREAK000000/1599612713000/image?w=240&h=240&bg_color=000000&opacity=100&_version=00_09_000",
"categories": [],
"release_date": null
},
{
"game_id": 11,
"title": "Marvel's Avengers",
"platform": "ps4",
"image": "https://store.playstation.com/store/api/chihiro/00_09_000/container/TR/en/999/EP0082-CUSA14030_00-BASEGAME0001SIEE/1599653581000/image?w=240&h=240&bg_color=000000&opacity=100&_version=00_09_000",
"categories": [],
"release_date": null
},
{
"game_id": 24,
"title": "The Suicide of Rachel Foster",
"platform": "ps4",
"image": "https://store.playstation.com/store/api/chihiro/00_09_000/container/TR/en/999/EP8923-CUSA19152_00-DAEEUTSORF000001/1599610166000/image?w=240&h=240&bg_color=000000&opacity=100&_version=00_09_000",
"categories": [
{
"category_id": 2,
"name": "Casual"
},
{
"category_id": 5,
"name": "İndie"
},
{
"category_id": 8,
"name": "Adventure"
}
],
"release_date": "2020-09-09"
},
{
"game_id": 25,
"title": "Takotan",
"platform": "ps4",
"image": "https://store.playstation.com/store/api/chihiro/00_09_000/container/TR/en/999/EP2005-CUSA24716_00-TKTN000000000000/1599610166000/image?w=240&h=240&bg_color=000000&opacity=100&_version=00_09_000",
"categories": [
{
"category_id": 1,
"name": "Action"
},
{
"category_id": 12,
"name": "Arcade"
},
{
"category_id": 13,
"name": "Shooter"
}
],
"release_date": "2020-09-09"...
Can you help me guys with this problem?I couldn't find any solution.

If you override the get method you have to make sure you know how the original implementation looks like.
def get(self,request,*args,**kwargs):
queryset = self.filter_queryset(self.get_queryset())
...
So filter_queryset is the missing piece here.

Related

Get Absolute URL of image using django-treebeard tree responce

Hello currently I implemented django-treebeard package for archive tree structure for categories.
Here I faced problem in response with URL of image.
I can't see Absolute URL of image categories
NOTE - serializer is not working with this recursive tree structure. I already know that you can pass
context = {'request':request} in serializer but in my case, it's not working because it is recursive tree structure.
views.py
class CategoryAPI(APIView):
def get(self, request):
try:
if CategoryModel.objects.count() > 0:
categories = CategoryModel.dump_bulk()
# serializer = CategorySerializer(instance=categories, many=True)
# print(serializer)
return Response({"status":True,"categories":categories}, status=status.HTTP_200_OK)
else:
return Response({'message': 'Categories not found.'}, status=status.HTTP_200_OK)
except Exception as e:
return Response({'error':str(e)})
API GET Response is like this
{
"status": true,
"categories": [
{
"data": {
"name": "Fruits",
"image": "Categories/download_3_Vg6Rv37.jfif"
},
"id": 1,
"children": [
{
"data": {
"name": "Apple",
"image": "Categories/download_6zbqDvn.jfif"
},
"id": 2,
"children": [
{
"data": {
"name": "Green Kasmiri Apple",
"image": "Categories/download_2_HaMCXyc.jfif"
},
"id": 10
}
]
},
{
"data": {
"name": "Banana",
"image": "Categories/download_4_Mn5h9nL.jfif"
},
"id": 3
},
{
"data": {
"name": "Mango",
"image": "Categories/download_5_4RkBFcS.jfif"
},
"id": 5
},
{
"data": {
"name": "Orange",
"image": "Categories/download_6_vWVEQgm.jfif"
},
"id": 4
}
]
},
{
"data": {
"name": "Health",
"image": "Categories/images_fwp1mBB.jfif"
},
"id": 6
}
]
}
I want to Responce is like this
{
"status": true,
"categories": [
{
"data": {
"name": "Fruits",
"image": "http://127.0.0.1:8000/media/Categories/download_3_Vg6Rv37.jfif"
},
"id": 1,
"children": [
{
"data": {
"name": "Apple",
"image": "http://127.0.0.1:8000/media/Categories/download_6zbqDvn.jfif"
},
"id": 2,
"children": [
{
"data": {
"name": "Green Kasmiri Apple",
"image": "http://127.0.0.1:8000/media/Categories/download_2_HaMCXyc.jfif"
},
"id": 10
}
]
},
{
"data": {
"name": "Banana",
"image": "http://127.0.0.1:8000/media/Categories/download_4_Mn5h9nL.jfif"
},
"id": 3
},
{
"data": {
"name": "Mango",
"image": "http://127.0.0.1:8000/media/Categories/download_5_4RkBFcS.jfif"
},
"id": 5
},
{
"data": {
"name": "Orange",
"image": "http://127.0.0.1:8000/media/Categories/download_6_vWVEQgm.jfif"
},
"id": 4
}
]
},
{
"data": {
"name": "Health",
"image": "http://127.0.0.1:8000/media/Categories/images_fwp1mBB.jfif"
},
"id": 6
}
]
}

Group queryset by field

I am working with Django and Django REST framework. I have a model called Selection that contains field called category, when i query the model to send the result to the frontend i get it with the following structure:
[
{
"id": 1,
"category": "SHOES",
"products": 122,
"created_at": "2021-09-11",
},
{
"id": 2,
"category": "SHOES",
"products": 4,
"created_at": "2021-10-07",
},
{
"id": 3,
"category": "CLOTHES",
"products": 0,
"created_at": "2021-10-08",
},
]
I need to put the selections of the same category in an array and remove the grouped-by category, like this:
{
"SHOES": [
{
"id": 1,
"products": 122,
"created_at": "2021-09-11",
},
{
"id": 2,
"products": 4,
"created_at": "2021-10-07",
}
],
"CLOTHES": [
{
"id": 3,
"category": "CLOTHES",
"products": 0,
"created_at": "2021-10-08",
}
]
}
I considered to making it with Javascript in the frontend, but before i wanted to know if there's a way to do this from Django.
Yes, you need to do some customisation in your code.
Get all categories by using values_list('category', flat=True) with your queryset
Iterate through them filtering category one by one
response_list = []
categories = Selection.objects.values_list('category', flat=True)
for category in categories:
data = Selection.objects.filter(category=category)
data = {
category: SelectionSerializer(data, many=True).data,
}
response_list.append(data)

what's the use if I am querying all the database before pagination in Django?

as per below code I am doing the query for database then I am paginating the query result. so My question what's the use if we already hit the database for all result before pagination. Is this like Django pagination working or I am pretending something wrong?
I am getting the response as expected but had question querying the database before pagination.
Code:
class GetAllUserStoryListViewTest(APIView,LimitOffsetPagination):
permission_classes = (IsAuthenticated,)
def get_object(self,user_id):
user = User.objects.filter(id = user_id).first()
posts = Post.objects.filter(user = user)
return posts
def get(self,request,user_id):
posts = self.get_object(user_id).order_by('-post_time_stamp')
#setting the limit of this user post pagination
LimitOffsetPagination.default_limit = settings.PAGE_SIZE
posts = self.paginate_queryset(posts, request)
serializer_context = {
'request': request,
}
serializer = PostListSerializer(posts,many=True,context=serializer_context)
# return Response(serializer.data)
return self.get_paginated_response(serializer.data)
output:
{
"count": 7,
"next": "http://127.0.0.1:8000/api/list_all_story_test/27/?limit=2&offset=5",
"previous": "http://127.0.0.1:8000/api/list_all_story_test/27/?limit=2&offset=1",
"results": [
{
"id": 15,
"file": "http://127.0.0.1:8000/media/user_27/post/IMG_20190331_144024.jpg",
"post_info": "#cool",
"post_time_stamp": "2020-05-10T10:21:10Z",
"total_comment": 2,
"liked_by": [
"vipin"
],
"user": {
"id": 27,
"username": "vipin",
"email": "vipinks#xyz.edu",
"status": true,
"profile": {
"id": 24,
"full_name": "Vipin",
"mobile": 6732,
"background_picture": "http://127.0.0.1:8000/media/user_27/profile/pexels-photo-531880_0qSgRNx.jpeg",
"profile_picture": "http://127.0.0.1:8000/media/user_27/profile/IMG_20190331_144024.jpg",
"BioDescription": "xyz",
"date_of_birth": "1996-06-01",
"gender": "M",
"account_type": "Private",
"user": 27
},
"last_login": null,
"is_superuser": false,
"is_staff": false,
"is_active": true,
"date_joined": "2020-04-28T11:09:27.691478Z"
},
"comment_usr": [
"xyz",
26,
"Cool Pic"
],
"like_by_you": true
},
{
"id": 13,
"file": "http://127.0.0.1:8000/media/user_30/post/IMG_20190402_102248.jpg",
"post_info": "#Awesome",
"post_time_stamp": "2020-05-10T10:20:22Z",
"total_comment": 8,
"user": {
"id": 30,
"username": "xyz",
"email": "xyz#gmail.com",
"status": false,
"profile": {
"id": 27,
"full_name": "XYZ",
"mobile": 123,
"background_picture": null,
"profile_picture": "http://127.0.0.1:8000/media/user_30/profile/demo.jpg",
"BioDescription": null,
"date_of_birth": null,
"gender": "F",
"account_type": "Private",
"user": 30
},
"last_login": null,
"is_superuser": false,
"is_staff": false,
"is_active": true,
"date_joined": "2020-04-28T11:13:58.030941Z"
},
"like_by_you": true
}
]
}
The pagination hit the database with Count() method, to paginating the result.
And, in every page hit the database by slicing that is more efficient than
using a queryset with all() method and, hit the database with Iteration, which load all the results.
filter() or all() methods don't hit the database. See this to check when a QuerySet is evaluated.

how to get records with its related data in intermediate table

I have two model, Category and Brand with many to many relation
in Category model
public function brands()
{
return $this->belongsToMany(Brand::class, 'brand_category', 'category_id', 'brand_id');
}
and in brand model
public function categories()
{
return $this->belongsToMany(Category::class, 'brand_category','brand_id','category_id' );
}
how can i get brands with specific category id like json below
{
"data": [
{
"brand_id": 1,
"name": "Sony",
"description": null,
"logo": null,
"is_active": true,
"created_at": "2020-04-08 15:19:44",
"updated_at": "2020-04-08 15:19:44",
"deleted_at": null,
"pivot": {
"category_id": 1,
"brand_id": 1
}
},
{
"brand_id": 2,
"name": "Lg",
"description": null,
"logo": null,
"is_active": true,
"created_at": "2020-04-08 15:19:44",
"updated_at": "2020-04-08 15:19:44",
"deleted_at": null,
"pivot": {
"category_id": 1,
"brand_id": 2
}
}
],
"success": true,
"error": {
"code": 200,
"data": []
},
"message": ""
}
i want to find a way without changing relations in models by wherePivot...
this should do.
return Category::find($categoryId)->brands;

Django rest framework CRUD API for model with foriegn key elements

Okay so here I am with another problem I'm facing with Django. This time it's the REST framework.
I have a Model with Foreign Key dependencies in my project. I had to retrieve the foreign key field as an object, not just an ID field and was successful in that using Serializer Relations.
So my Serializer.py looks like:
class EventSerializer(serializers.ModelSerializer):
owner = EventUserSerializer(read_only=False)
severity = EventSeveritySerializer(read_only=False)
type = EventTypeSerializer(read_only=False)
cause = EventCauseSerializer(read_only=False)
subcause = EventSubcauseSerializer(read_only=False)
impact = EventImpactSerializer(read_only=False)
point_location = PointLocationSerializer(read_only=False)
class Meta:
model = models.Event
fields = ('id', 'description', 'owner', 'status',
'severity', 'type', 'cause', 'subcause', 'impact',
'is_public', 'occurrence_time', 'reporting_agency',
'estimated_duration', 'confirmed_timestamp',
'rejected_timestamp', 'closed_timestamp', 'point_location', )
Making a GET request, I get the result I wanted:
{
"meta": {
"count": 1,
"previous": null,
"next": null
},
"results": [
{
"id": 8,
"description": "test event 1",
"owner": {
"user": {
"id": 1,
"username": "venus",
"email": "venus.saini#ibigroup.com"
}
},
"status": "confirmed",
"severity": {
"id": 2,
"created": "2016-08-24T07:27:24.722000Z",
"modified": "2016-08-24T07:27:24.723000Z",
"name": "Severe",
"is_enabled": true,
"sortorder": 1
},
"type": {
"id": 2,
"created": "2016-08-24T07:27:45.203000Z",
"modified": "2016-08-24T07:27:45.204000Z",
"name": "Accident",
"event_category": "unplanned",
"sortorder": 2
},
"cause": {
"id": 2,
"created": "2016-08-24T07:28:16.088000Z",
"modified": "2016-08-24T07:28:16.092000Z",
"name": "Whether Conditions",
"sortorder": 3,
"event_type": 2
},
"subcause": {
"id": 2,
"created": "2016-08-24T07:28:28.475000Z",
"modified": "2016-08-24T07:28:28.478000Z",
"name": "Slippery",
"display_name": "Slippery",
"sortorder": 4,
"cause": 2
},
"impact": {
"id": 2,
"created": "2016-08-24T07:28:40.599000Z",
"modified": "2016-08-24T07:28:40.599000Z",
"name": "All Lanes Blocked",
"sortorder": 5
},
"is_public": false,
"occurrence_time": "2016-08-25T06:34:13Z",
"reporting_agency": "",
"estimated_duration": null,
"confirmed_timestamp": "2016-08-25T06:34:15Z",
"rejected_timestamp": "2016-08-25T06:34:16Z",
"closed_timestamp": "2016-08-25T06:34:18Z",
"point_location": {
"id": 2,
"created": "2016-08-24T07:18:03.017000Z",
"modified": "2016-08-24T07:18:03.018000Z",
"roadway_name": "Northbound",
"direction": "North",
"is_primary": false,
"cross_street_name": "test street 1",
"offset": "After",
"distance": 3,
"is_forward": false
}
}]
}
The problem I face is while using the POST request. I don't know how that would work so I found this link. It looks like it has the solution; I mean this is exactly what I want to accomplish [passing the ID of the foreign key element makes the object entry]. But I keep getting random errors like super(BaseSerializer, self).__init__(**kwargs) TypeError: __init__() got an unexpected keyword argument 'is_relation'.
Can somebody please guide me to how I should be doing it? I'm using Postman to test my APIs.
Thanks in advance.
EDIT: Using PUT/POST without the CustomSerializer gives me error 415:
{
"detail": "Unsupported media type \"text/plain;charset=UTF-8\" in request."
}