Search code examples
pythondjangodatabaseviewmodel

how to use model in django view


I want to use my model parameter for the API query parameter in Django View. How can I use this? Anyone can give me suggestions. Here curd is my model name. and consumer_type is the required query parameter

  from .models import curd
  for consumer_type in curd:
  response = requests.get("http://localhost:8280?ConsumerID="+consumer_type)

Solution

  • In the view, you can access the GET request like so:

    if request.method == "GET":
        print(request.GET)
    

    Then you could filter with:

    cons_id = request.GET.get('ConsumerID', None)
    if cons_id is None:
        raise Http404
    else:
        mymodel = MyModel.objects.filter(...)