0

我想编写所有类型的复杂查询,例如:

如果有人想要信息“水果”是“浦那区”的“番石榴”,那么他们将获得浦那区番石榴的数据。

htt//api/?fruit=Guava&?district=Pune

如果有人想要信息“水果”是“Girnare Taluka”中的“Guava”,那么他们将获得 girnare taluka 中的番石榴数据。

htt://api/?fruit=Guava&?taluka=Girnare

如果有人想要“水果”的信息是“番石榴”和“香蕉”,那么他们将只获得这两种水果的所有数据,同样明智

htt://api/?fruit=Guava&?Banana

但是,当我运行服务器时,我无法获得正确的输出。如果我使用http://api/?fruit=Banana ,那么我会得到所有水果的数据,即香蕉、石榴、番石榴,而不是获取水果的数据只是香蕉。所以我很困惑这里发生了什么。你能检查我的代码,我在哪里出错了?*这是我所有的文件

模型.py

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 get_wbcis(fruit=None, district=None, talkua=None, revenue_circle=None, sum_insured=None, area=None,min_farmer=None, max_farmer=None, limit=100):

    query = Wbcis.objects.all()

    if fuit is not None:

        query = query.filter(Fruit=fruit)

    if district is not None:

        query = query.filter(District=district)

    if taluka is not None:

        query = query.filter(Taluka=taluka)

    if revenue_circle is not None:

        query = query.filter(Revenue_circle= revenue_circle)

    if sum_insured is not None:

        query = query.filter(Sum_Insured=sum_Insured)

    if area is not None:

        query = query.filter(Area=area)

    if min_farmer is not None:

        query = query.filter(Farmer__gte=min_farmer)

    if max_farmer is not None:

        query = query.filter(Farmer__lt=max_farmer)
    return query[:limit]

视图.py

class WbcisViewSet(ModelViewSet):

    queryset = Wbcis.objects.all()

    serializer_class = WbcisSerializer


def wbcis_view(request):

    fruit = request.GET.get("fruit")

    district = request.GET.get("district")

    taluka = request.GET.get("taluka")

    revenue_circle = request.GET.get("revenue_circle")

    sum_insured = request.GET.get("sum_insured")

    area = request.GET.get("area")

    min_farmer = request.GET.get("min_farmer")

    max_farmer = request.GET.get("max_farmer")

    wbcis = get_wbcis(fruit, district, taluka,revenue_circle,sum_insured,area, min_farmer, max_farmer)


    #convert them to JSON:

    dicts = []

    for wbci in wbcis:

        dicts.append(model_to_dict(wbci))

    return JsonResponse(dicts)

序列化器.py

from rest_framework.serializers import ModelSerializer

from WBCIS.models import Wbcis



class WbcisSerializer(ModelSerializer):


    class Meta:

        model = Wbcis

        fields=('id','Fruit','District','Sum_Insured','Area','Farmer','Taluka','Revenue_circle',)

调用这些查询以获得准确的输出需要在此代码中进行哪些更改?

4

1 回答 1

0

我不认为您实际上是在调用该视图,从您的使用情况来看,我认为您是在调用视图集本身,然后忽略查询参数。

您应该按照drf 文档进行过滤,但本质上,将 get queryset 方法提供给您的视图集,并在其中包含您当前在视图中拥有的代码

class WbcisViewSet(ModelViewSet):

    queryset = Wbcis.objects.all()  # Shouldn't need this anymore

    serializer_class = WbcisSerializer


    def get_queryset(self):

        fruit = self.request.query_params.get("fruit")
        ....
        return get_wbscis(...)
于 2016-12-22T09:49:59.097 回答