我有geojson格式的数据。
这是我的数据:
{
"type" : "FeatureCollection",
"features" : [
{
"type" : "Feature",
"properties" : {
"year" : 2015,
"season" : "rabbi",
"crop" : "banana",
"district" : "pune",
"taluka" : "haveli",
"circle" : "uralikanchan",
"farmer" : 100
},
"geometry" : {
"type" : "Polygon",
"coordinates" : [
[
74.129992,
18.505494
],
[
74.129047,
18.505494
],
[
74.128275,
18.504436
],
[
74.127588,
18.503052
],
[
74.114456,
18.498331
],
[
74.113941,
18.498331
],
[
74.112482,
18.493773
],
[
74.112053,
18.491494
],
[
74.143724,
18.473992
],
[
74.144497,
18.474888
],
[
74.145269,
18.476027
],
[
74.15617,
18.486854
],
[
74.155912,
18.488319
],
[
74.145956,
18.502564
],
[
74.129992,
18.505494
]
]
}
}
]
}
views.py文件:
from rest_framework_mongoengine.viewsets import ModelViewSet as MongoModelViewSet
from app.serializers import *
from rest_framework_mongoengine.generics import *
def index_view(request):
context = {}
return TemplateResponse(request, 'index.html', context)
class ToolViewSet(MongoModelViewSet):
serializer_class = ToolSerializer
my_filter_fields = ('type','features','geometry',) # specify the fields on which you want to filter
def get_kwargs_for_filtering(self):
filtering_kwargs = {}
for field in self.my_filter_fields: # iterate over the filter fields
field_value = self.request.query_params.get(field) # get the value of a field from request query parameter
if field_value:
if ',' in field_value: # put your queryParams into an array and use the built-in django filter method '__in'
filtering_kwargs[field + '__in'] = field_value.split(',')
else:
filtering_kwargs[field] = field_value
return filtering_kwargs
def get_queryset(self):
queryset = Tool.objects.all()
filtering_kwargs = self.get_kwargs_for_filtering() # get the fields with values for filtering
if filtering_kwargs:
queryset = Tool.objects.filter(**filtering_kwargs) # filter the queryset based on 'filtering_kwargs'
return queryset
此动态查询适用于字段"类型","功能"和"几何"。 但是,我必须使用我的属性字段显示数据 (即:年,季节,作物,区,taluka。这些字段中的所有字段{property {" crop"," district",.....}}
我需要更改此代码中的工作字典字段 然后,如果我去了服务器http://api/tool/?crop=banana,那么它会显示这些数据吗?