数据:
{
"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
}
我想编写所有类型的组合查询,如果有人只想获得Guava的信息,那么输出将只是Guava的精确数据。
如果有人想要只有香蕉和水果的水果信息呢?番石榴然后输出将是香蕉和番石榴的确切数据。
如果水果等于香蕉
output will be data for Banana
如果水果等于番石榴
output will be data for Guava
如果水果等于香蕉和番石榴
output will be data for Banana and Guava
此外,如果有人想要只为Nasik区提供信息,那么输出将仅为Nasik区的确切数据。 查询“区”
如果区域等于纳西克
output will be data for Nasik District
如果区等于Nanded
output will be data for Nanded District
同样,还有“Revenue_circle,Farmer等。
的查询我知道如何使用find
在mongoshell中编写此查询 db.collections.find({"District":"Nasik"})
等......
但我想在python脚本中编写查询。
你能帮我解决这个问题吗?任何提示?我在Models.py文件中尝试写过
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
我尝试在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()
这是正确的方法吗?或者需要对models.py和views.py文件进行一些更改?
答案 0 :(得分:1)
您需要在django中创建模型,以便您可以使用django的ORM轻松访问数据。以下是Django models的一些信息:
在上面,您的模型将类似于:
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()
然后你就可以进行如下查询:
from models import Item
guava_items = Item.objects.filter(fruit='Guava')
print guava_items
# Out: [<guava item>, <guava item>, ...]