Reputation: 3897
I have this view:
def getData():
response = requests.get("https://path.to/api/")
data = response.json()
random_data = [MyModel(name=name, street=street, email=email, phone=phone) \
for name, street, email, phone, in data.items()]
MyModel.objects.bulk_create(random_data)
@require_http_methods(['GET', 'POST'])
def index(request):
datas = getData()
return render(request, 'index.html')
When I access the specified url for index
method, it throws me this:
Traceback (most recent call last):
File "/home/user/.virtualenvs/myproj/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/user/.virtualenvs/myproj/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/user/.virtualenvs/myproj/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/user/.virtualenvs/myproj/lib/python3.6/site-packages/django/views/decorators/http.py", line 40, in inner
return func(request, *args, **kwargs)
File "/home/user/django_projects/myproj/myproj/user/views.py", line 29, in index
datas = getData()
File "/home/user/django_projects/myproj/myproj/user/views.py", line 18, in getData
for name, street, email, phone, in data.items()]
File "/home/user/django_projects/myproj/myproj/user/views.py", line 18, in <listcomp>
for name, street, email, phone, in data.items()]
ValueError: not enough values to unpack (expected 4, got 2)
[18/Aug/2019 15:18:05] "GET /api/index/ HTTP/1.1" 500 85435
Method Not Allowed (HEAD): /api/index/
From this error, I think that the resulting json response from the api might be the problem, since some of these items are kind of nested
into the resulting json
Here's an example response:
Any ideas?
Upvotes: 1
Views: 2384
Reputation: 1540
can you check this solution !!
def getData():
response = requests.get("https://path.to/api/")
data = response.json()
random_data = [MyModel(name=x['name']['first'], streer=x['location']['street'], email=x['email'], phone=x['phone']) for x in data['results']]
MyModel.objects.bulk_create(random_data)
Upvotes: 1