Click here to Skip to main content
15,670,482 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone, I am currently learning Djnago and situation is the following. I have a legacy database, I connected it to Django. In this database I have names of schools. I have written a code in python that uses those names and returns lat and lng from Google API.
Here is the code:
def read(file_name):
    school_list = []
    with open(file_name) as f:
        gmaps = googlemaps.Client(key='my google key')
        for o in data:
                geocode_result = gmaps.geocode(o['Skolenhetsnamn'])
            except IndexError:
            lat=location.get ("lat","0.0")
            school = School(o['Skolenhets-kod'], o['Skolenhetsnamn'], o['Kod'], o['Kommun'],lat,lng)
    return school_list

It perfectly works with json file outside Django . However, I wanted to do the same in Django in python shell. Calling "python shell" and update my model. In other words add to the database lat and lng using the same python code,but in Django database API.That does not seem to work. Should I modify somehow the code for Django? Or it is better to update database out of Django and attach the database with the full info? Will be grateful for any hints.

What I have tried:

To check Django documentation about creating objects in databases
Richard MacCutchan 5-Jan-19 4:21am    
"That does not seem to work."
Exactly what does that mean?
Member 14108446 5-Jan-19 7:39am    
Hi, sorry, it is a wrong formulation. The code itself works in the shell, but does not do what I want. I have a model Skola in Django and two empty objects lat, lon. So basically I wanted the new values to be generated from google API and attached to the django model. This code I have returns only one value and even though I am using save() it does not seem to be saved. I was wondering if there are any special django functions to iterate through django model taking each object one by one and attach new values to the existing model
In addition in Django I have an sql database attached, I do not use jason file there.
Richard MacCutchan 5-Jan-19 11:37am    
"The code itself works in the shell, but does not do what I want."
Er, that means it does not work. But either way there is nothing in the above that gives much of a clue of anything.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900