Is there a way to have a user enter a url query and have a single function filter a database?

21 views Asked by At

I'm playing with the Django rest framework and PostgreSQL, building an API that returns data on all of the stats from this NBA season. Rather than having a function to return stats for each category, such as:

 @api_view(['GET'])
def sortByRebounds(request):
    """Returns all stats in the database sorted by rebounds per game"""
    string = Nba_stats.objects.values_list('name', 'team', 'rpg').order_by('rpg').reverse()
    return Response(string)

It possible to have a single function capable of filtering and returning data based on what the user types in the url?

For example a function called sortBy that takes in a category as a parameter and a url "api/sortby/<str:category>/" where a user can type "api/sortby/rebounds" and receive the data on rebounds or "api/sortby/assists" and receive everything on assists.

0

There are 0 answers