2

Data:

{
  "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
}

I want to write all types of combination queries, if someone wants information only for Fruit which is Guava then the output will be exact data for Guava only.

also if someone wants information only for Fruit which is Banana & Guava then the output will be exact data for Banana and Guava.

If fruit is equal to Banana

  output will be data for Banana

If fruit is equal to Guava

  output will be data for Guava

If fruit is equal to Banana and Guava

  output will be data for Banana and Guava

Also, if someone wants information only for District which is Nasik then the output will be exact data for Nasik district only. Query for "District"

If District is equal to Nasik

  output will be data for Nasik District

If District is equal to Nanded

 output will be data for Nanded District

likewise, there is query for "Revenue_circle, Farmer etc.

I know how to write this queries in mongoshell using find

db.collections.find({"District":"Nasik"}) etc...

but I want to write the query in the python script.

Can you please help me to solve this problem? Any Hints?

I tried and write in Models.py file

from django.contrib.auth.models import User   
from django.db import models    
from django.db.models import Q

class Wbcis(models.Model):

    Fruit = models.CharField(max_length=50)

    District = models.CharField(max_length=50)

    Taluka = models.CharField(max_length=50)

    Revenue_circle = models.CharField(max_length=50)

    Sum_Insured = models.FloatField()

    Area = models.FloatField()

    Farmer = models.IntegerField()


    def __str__(self):

        return self.Fruit

    def save(self, *args, **kwargs):

        super().save(*args, **kwargs)

    class Meta:

      verbose_name_plural = 'wbcis'


from models import Wbcis
Guava =Wbcis.objects.filter(Q(Fruit='Guava'))    
print Guava    
Banana= Wbcis.objects.filter(Q(Fruit='Banana'))    
print Banana        
Pomegranate= Wbcis.objects.filter(Q(Fruit='Pomegranate'))    
print Pomegranate
Guava_Banana=Wbcis.objects.filter(Q(Fruit='Guava')&Q(Fruit='Banana'))  
print Guava_Banana
Guava_Pomegranate=Wbcis.objects.filter(Q(Fruit='Guava')&Q(Fruit='Pomegranate'))
print Guava_Pomegranate

I tried an I write in Views.py

from rest_framework.viewsets import ModelViewSet
from WBCIS.serializers import WbcisSerializer
from WBCIS.models import Wbcis 
from rest_framework.filters import SearchFilter
from django.db.models import Q


class WbcisViewSet(ModelViewSet):

    queryset = Wbcis.objects.all()

    serializer_class = WbcisSerializer

    filter_backends=[SearchFilter]

    search_fields=['Fruit','District','Sum_Insured','Area','Farmer','Taluka','Revenue_circle','id']   

    def get_queryset(self, *args, **kwargs):

        queryset_list =Wbcis.objects.all()

        queryset_list1 =Wbcis.objects.all()

        query =self.request.GET.get("Fruit")

        query1=self.request.GET.get("District")

        query2=self.request.GET.get("Taluka")


        if query and query1:

            queryset_list = queryset_list.filter(
            Q(Fruit__icontains=query)
            |Q(Distirct__icontains=query1)
            )

            return queryset_list

        elif query1:

            queryset_list1 = queryset_list.filter(
            Q(District__icontains=query1)
            )  

            return queryset_list1

        elif query:

            queryset_list1 = queryset_list.filter(
            Q(Fruit__icontains=query)
            )  

            return queryset_list1

        elif query and query2:

            queryset_list2 = queryset_list.filter(
            Q(Fruit__icontains=query)&
            Q(Taluka__icontains=query2)
            )  

            return queryset_list2

        return Wbcis.objects.all()

Is this correct way? or need some changes in models.py and views.py file?

2
  • 1
    Are you Using the django ORM for your data? Commented Dec 20, 2016 at 7:36
  • Yes, I tried, Also I use django_mongoengine , but I want to writing a queires in python script which is ultimately going to call these queries from django. So,I am confuse Commented Dec 20, 2016 at 7:40

1 Answer 1

1

You will want to create a model in django so that you can use django's ORM to easily access the data. Here's some information on Django models:

In the above, your model would be something like:

class Item(models.Model):
    Fruit = models.CharField(max_length=60)
    District = models.CharField(max_length=60)
    Taluka = models.CharField(max_length=60)
    RevenueCircle = models.CharField(max_length=60)
    SumInsured = models.IntegerField()
    Area = models.IntegerField()
    Farmer = models.IntegerField()

Then you'd be able to do a query like:

from models import Item
guava_items = Item.objects.filter(fruit='Guava')
print guava_items
# Out: [<guava item>, <guava item>, ...]
Sign up to request clarification or add additional context in comments.

10 Comments

Okay, if some one want information for banana_items than how to add another query ? need to use while loop?
@KiranPrajapati sure, you could have a for fruit in ['Banana', 'Guava', 'Pomegranate']:
I am confuse in while loop, can you give me idea for fruit using while loop?
So, my all queries write in models.py file?
@KiranPrajapati it's too hard to read your code in the comment. Let's ask a new question?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.