1👍
I’m not sure what you want, but your queries seem correct.
I think what you mean with your for
loop, is that you can iterate over a query (technically, a QuerySet
) to get the results.
For example:
Guava = Wbcis.objects.filter(Q(Fruit='Guava'))
for guava_entry in Guava:
# Do something with the returned element here:
print guava_entry
If you want to filter by multiple things, you can just have multiple conditions in your filter. For example, to get Bananas in Nanded
Guava = Wbcis.objects.filter(Fruit="Banana", District="Nanded")
You don’t need to use a for
loop for that. By default, filter
combines your conditions using and
. That means that in the example above, Fruit must be “Banana”, and District must be “Nanded”.
Looking at the examples you have in your question, you have this query:
Guava_Banana=Wbcis.objects.filter(Q(Fruit='Guava')&Q(Fruit='Banana'))
This query will never return any results. It is asking for an object that has both a Fruit of “Guava” and a Fruit of “Banana”. What you want instead is this:
Guava_Banana=Wbcis.objects.filter(Q(Fruit='Guava') | Q(Fruit='Banana'))
This new query returns objects where fruit is either “Guava” or “Banana”.
I understand this can be confusing, because you want to return Guavas and Bananas, but think of it as a boolean expression.
You can wrap this in a function like this:
def get_wbcis(fruit=None, district=None, talkua=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 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]
The limit
parameter ensures that at most that many results are returned.
min_farmer in this example uses the __gte
query operator, meaning that results with a Farmer greather than or equal to min_farmer
will be returned.
max_farmer uses the __lt operator, so results with a farmer lower than, but not equal to max_farmer
will be returned.
Similarly, you could use __gt or __lte if you want different different inequality filters.
Your views.py
could do something like this:
import json
from django.forms.models import model_to_dict
from django.http import JsonResponse
from models import get_wbcis
def wbcis_view(request):
fruit = request.GET.get("fruit")
district = request.GET.get("district")
taluka = request.GET.get("taluka")
min_farmer = request.GET.get("min_farmer")
max_farmer = request.GET.get("max_farmer")
wbcis = get_wbcis(fruit, district, taluka, min_farmer, max_ffarmer)
#convert them to JSON:
dicts = []
for wbci in wbcis:
dicts.append(model_to_dict(wbci))
return JsonResponse(dicts)