I have set up the column where one of the table is called Establishment_Type
Now, I am trying to filter according to Establishment_Type.
Here is my view.py code
class ShopDetailAPIView(ListAPIView):
serializer_class = ShopDetailSerializer
def get_queryset(self):
queryset = Shop.objects.all()
type = self.request.query_params.get('type', None)
type2 = self.request.query_params.get('type2', None)
if type is not None and type2 is None:
queryset = queryset.filter(Establishment_Type = type)
elif type is not None and type2 is not None:
queryset = queryset.filter(Q(Establishment_Type = type) | Q(Establishment_Type = type2))
return queryset
In the url, I query by typing:
http://127.0.0.1:8000/shop/search/?type=Restaurant&type2=Petrol%20Station
Which only filter Establishment_Type = Restaurant but not include Establishment_Type = Petrol Station
Here is my urls.py within my app called shop:
urlpatterns = [
url(r'^$', ShopListAPIView.as_view(), name = 'list' ),
#####
url(r'^create/$', ShopCreateAPIView.as_view(), name = 'create' ),
url(r'^search/$', ShopDetailAPIView.as_view(), name = 'detail'),
]
Was my url for filtering 2 Establishment type wrong?
Do I need to change something in my code in order to filter 2 values in column Establishment_Type?
type2.queryset.filter(Establishment_Type__in=types), where types is list of your not None types..get('key', None)use just.get('key'). It's the same.