I want to writing a queires in python script which is ultimately going to call these queries from django

104 views Asked by At

Here is my mongodb database :

**{"Fruit" : "Pomegranate", "District" : "Nasik", "Taluka" : "Nasik", "Revenue circle" : "Nasik", "Sum Insured" : 28000, "Area" : 1200, "Farmer" : 183 }

{"Fruit" : "Pomegranate", "District" : "Jalna", "Taluka" : "Jalna", "Revenue circle" : "Jalna", "Sum Insured" : 28000, "Area" : 120, "Farmer" : 13 }

{"Fruit" : "Guava", "District" : "Pune", "Taluka" : "Haveli", "Revenue circle" : "Uralikanchan", "Sum Insured" : 50000, "Area" : 10, "Farmer" : 100 }

{"Fruit" : "Guava", "District" : "Nasik", "Taluka" : "Girnare", "Revenue circle" : "Girnare", "Sum Insured" : 50000, "Area" : 75, "Farmer" : 90 }

{"Fruit" : "Banana", "District" : "Nanded", "Taluka" : "Nandurbar", "Revenue circle" : "NandedBK", "Sum Insured" : 5000, "Area" : 2260, "Farmer" : 342 }

{"Fruit" : "Banana", "District" : "Jalgaon", "Taluka" : "Bhadgaon", "Revenue circle" : "Bhadgaon", "Sum Insured" : 5000, "Area" : 220, "Farmer" : 265 }**

Here is my models.py file :

from mongoengine import Document,  fields
from django.db.models import Q


class Tool(Document):
    Fruit = fields.StringField(required=True)
    District = fields.StringField(required=True)
    Taluka = fields.StringField(required=True)
    Revenue_circle = fields.StringField(required=True)
    Sum_Insured = fields.StringField(required=True)
    Area = fields.StringField(required=True)
    Farmer = fields.StringField(required=True)

Here is my serializers.py file :

from rest_framework import serializers
from rest_framework_mongoengine.serializers import DocumentSerializer
from models import Tool


class ToolSerializer(DocumentSerializer):
    #id = serializers.CharField(read_only=False)
    class Meta:
        model = Tool
        #fields=['Fruit']

Here is my views.py file

from __future__ import unicode_literals
from django.template.response import TemplateResponse
from rest_framework_mongoengine.viewsets import ModelViewSet as MongoModelViewSet
from app.serializers import *


def index_view(request):
     context = {}
    return TemplateResponse(request, 'index.html', context)


class ToolViewSet(MongoModelViewSet):
    lookup_field = 'Fruit'
    serializer_class = ToolSerializer

    def get_queryset(self):
        return Tool.objects.all()

Now, I want to writing queires, But when I use lookup filed='Fruit' in views.py file then http://127.0.0.1:8000/api/tool/Banana/
I get All information for fruit which is banana .

127.0.0.1:8000/api/tool/Guava/ then I get All information for fruit which is guava.

lookup filed is use for single fields, But,I want to write all types of combination queires. for example: /api/tool/Guava/ (gives all data for fruit is guava)

/api/tool/Nasik/ (gives all data for district is Nasik)

/api/tool/Uralikanchan/ (gives all data for revenue_circle is uralikanchan)

/api/tool/265 (gives all data for farmer is 265)

/api/tool/2260/ (gives all data for Area is 2260 )

So,can you please tell me how to write all types of combination queries which call these queires to django?

2

There are 2 answers

0
AudioBubble On BEST ANSWER

you can use rest-framework-filter-backend ,create Filterset see this doc http://www.django-rest-framework.org/api-guide/filtering/

0
Linovia On

You need to override the view's get_queryset to perform the request you want on the fields you want.