an interface: http://127.0.0.1:8000/api/v1/course/ corresponds to the following processing view
class CourseView(APIView):
-sharp renderer_classes = [JSONRenderer,]
def get(self, request, *args, **kwargs):
ret = {"code": 1}
try:
queryset = models.Course.objects.all()
ser = CourseSerializer(instance=queryset, many=True)
ret["data"] = ser.data
except Exception as e:
ret["code"] = 0
ret["msg"] = ""
return Response(ret)
the interface takes no parameters and returns all the data of .all ().
then the problem arises: if I want to write an interface api/products/, this interface, if there are three optional parameters, such as
product name: name (can be vaguely queried), product type: type; production time: time.
if none of the parameters are taken, name returns .all () all data,
how to deal with this problem?
my current idea is:
1 / judge whether to take parameters or not. If there are no parameters, return .all ()
2 / if you have one or more parameters, use .filter (key1 = value1,key2=value2.) Check the data in this form.
thought for a moment, if it is too complicated to judge the parameter logic in this form? Maybe my idea is wrong. Because serializer, such as the above CourseSerializer object instance parameter, needs to pass in a .all () queryset object.
restframework has no components that handle queries like Filter and fuzzy search queries. Do you have any information above?
< hr >