简体   繁体   中英

How do I structure my view for sending an api get request in Django?

I am sending a get request in my view and then using the response to fill my database and I need some confirmation on the following:

should i make an api call inside of a view?

what should that view response be?

if i have done it wrong then what would be the right way to send get requests in Django?

my_app/views.py

class api(APIView):
  template_name = 'payment/api.html'

  def get(self, request):
    #I SEND THE GET REQUEST HERE
    config = configparser.ConfigParser()
    config.read('config.ini')
    r = requests.get(config['DEFAULT']['api'])
    response = r.json()

    #HERE I FILTER THE RESPONSE AND PUT IN A DB
    for item in response:
      if 'Covered Recipient Physician' in item.values():
        person, _ = models.Person.objects.get_or_create(
        profile_id = int(item['physician_profile_id']),
        first_name = item['physician_first_name'].lower(),
        last_name = item['physician_last_name'].lower()
        )
         address, _ = models.Address.objects.get_or_create(
         business_street = 
item['recipient_primary_business_street_address_line1'].lower(),
      city = item['recipient_city'].lower(),
      state = item['recipient_state'].lower(),
      country = item['recipient_country'].lower()
    )
    business, _ = models.Business.objects.get_or_create(
      business = item['submitting_applicable_manufacturer_or_applicable_gpo_name'].lower(),
    )
    business_address_link = models.Business_address_link.objects.create(
      business = business,
      address = address
    )
    business_address_link.save()
    payment = models.Payment.objects.create(
      record_id = int(item['record_id']),
      amount = float(item['total_amount_of_payment_usdollars']),
      date = item['date_of_payment'],
      number_of_payments = int(item['number_of_payments_included_in_total_amount']),
      payment_form = item['form_of_payment_or_transfer_of_value'],
      nature_of_payment = item['nature_of_payment_or_transfer_of_value']
    )
    payment.save()
    person_payment_information = models.Person_payment_information.objects.create(
      person = person,
      business_address_link = business_address_link,
      payment = payment
    )
    person_payment_information.save()

Try to use this function for GET request -

@require_http_methods(['GET'])
def get_persons(request):
    try:
        data = list(Message.objects.all.values())
        return render(request=request, template_name='template.html', context={"data": data})
    except Exception as e:
        return render(request=request, template_name='template.html', context={"error": e})

Once you get the response and send it to template.html, you can display it in any way you want.

If you want to add information to the DB you better use POST request, for example -

@require_http_methods(['POST'])
def add_person(request):
    try:
        data = json.loads(request.body)
        new_person = Person(**data)
        new_person.save()
        return render(request=request, template_name='template.html', context={"data": data})
    except Exception as e:
        return render(request=request, template_name='template.html', context={"error": e})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM