Get data from external URL in JSON format with Django REST

wolnio

I want to do simple Rest API in Django. It's my first approach to Rest in django, but for now it isn't going that bad ;)

For now I have a model database with two fields brand and model. With the use of Postman, I can GET, POST and DELETE objects, cool.

But what I'm stuck with is -> When I POST an object, first I'd like to check if that model with given brand even exists. I want to check it on specific website: link. (In the URL of that website I can change brand name so it gives me a list of model with given brand)

My approach would be, when request.method=='POST' first GET data from website, load it to a model and then somehow compare it with data in POST request. But I don't know how to get to that point :)

Is it a good approach or is there a way to directly look at data on the given website?

(For me there is no need to give you my code, because there is nothing special in it, but tell me if I'm wrong!)

Marco

Given your information you can handle this with the following approach:

import requests

API_URL = "https://vpic.nhtsa.dot.gov/api/vehicles/getmodelsformake/audi?format=json"
SEARCH_FOR_MODEL = "RS5"

try:
    r = requests.get(API_URL)
    models = r.json()
except:
    logging.WARNING(f"Fetch models from {API_URL} failed")

result = next(
    (model for model in models["Results"] if model["Model_Name"] == SEARCH_FOR_MODEL),
    None,
)

if not result is None:
    print(f"Model {SEARCH_FOR_MODEL} found")
else:
    print(f"Model {SEARCH_FOR_MODEL} not found")

This is based on the link you provide for the API endpoint. They may also offer a filter function for models. Then it is not necessary to search the entire response.

Searching for values in a dictionary from this post: https://stackoverflow.com/a/8653568/4151233

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get Json Object from external URL with PHP

From Dev

Get data from external link of JSON by jQuery

From Dev

Django Rest get file from FileField url

From Dev

I have to get data from the following url which contains the data in json format

From Dev

Importing Json into Django Database from url only imports parts of the data and says the rest is up to date but it isn't

From Dev

Displaying data from a json array from external url with no api

From Dev

To get data from the list in Required JSON format

From Dev

How to get JSON from external URL using JavaScript (jQuery) Ajax?

From Dev

Problem parsing JSON data from external url in PHP

From Dev

How do I load data from an external URL using JSON?

From Dev

HttpClient: Can't get data from external URL

From Dev

How to get data from a URL and convert it into json?

From Dev

ReactJS - get json object data from an URL

From Dev

Get data from URL json in R

From Dev

Get full request URL from inside APIView in Django REST Framework

From Dev

How to get pk from url in django rest framework?

From Dev

Restful API URL to get json data for different file type format

From Dev

Django get value from JSON data

From Dev

Browserify get external JSON data

From Dev

I can't get JSON objects from a REST API URL

From Dev

JSON data not coming through from Django REST API

From Dev

angulardart and django_rest. unable to get data from the backend

From Dev

How Get data with join django Rest framework from other table

From Dev

django rest framework get data from foreign key relation

From Dev

How can to get data from related model in Django Rest Framework?

From Dev

How to get data related from another table django rest framework

From Dev

Django rest framework get data from foreign key relation?

From Dev

Get data from external api

From Dev

Is it possible to create a Django app which only reads data from external database and provides REST API?

Related Related

  1. 1

    Get Json Object from external URL with PHP

  2. 2

    Get data from external link of JSON by jQuery

  3. 3

    Django Rest get file from FileField url

  4. 4

    I have to get data from the following url which contains the data in json format

  5. 5

    Importing Json into Django Database from url only imports parts of the data and says the rest is up to date but it isn't

  6. 6

    Displaying data from a json array from external url with no api

  7. 7

    To get data from the list in Required JSON format

  8. 8

    How to get JSON from external URL using JavaScript (jQuery) Ajax?

  9. 9

    Problem parsing JSON data from external url in PHP

  10. 10

    How do I load data from an external URL using JSON?

  11. 11

    HttpClient: Can't get data from external URL

  12. 12

    How to get data from a URL and convert it into json?

  13. 13

    ReactJS - get json object data from an URL

  14. 14

    Get data from URL json in R

  15. 15

    Get full request URL from inside APIView in Django REST Framework

  16. 16

    How to get pk from url in django rest framework?

  17. 17

    Restful API URL to get json data for different file type format

  18. 18

    Django get value from JSON data

  19. 19

    Browserify get external JSON data

  20. 20

    I can't get JSON objects from a REST API URL

  21. 21

    JSON data not coming through from Django REST API

  22. 22

    angulardart and django_rest. unable to get data from the backend

  23. 23

    How Get data with join django Rest framework from other table

  24. 24

    django rest framework get data from foreign key relation

  25. 25

    How can to get data from related model in Django Rest Framework?

  26. 26

    How to get data related from another table django rest framework

  27. 27

    Django rest framework get data from foreign key relation?

  28. 28

    Get data from external api

  29. 29

    Is it possible to create a Django app which only reads data from external database and provides REST API?

HotTag

Archive