Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

saving json data to a database in django

  • 16-11-2018 03:36PM
    #1
    Registered Users, Registered Users 2 Posts: 10,794 ✭✭✭✭


    Hi folks

    my heads wrecked with this. Im finishing a part-time hdip and Im trying to pull my final project together. Ive pulled latitude and longitude data from an API. I can extract the data I need and I can disply it on a page, using django. No issues with that.

    What I want to do is save the info to a data base. I've simplified it down to just saving the latitude info. As I say, I can print the info no problem. i have it stored in a variable called 'lat'.
    I set up a model with this info:
    from django.db import models
    
    class latlon(models.Model):
        lat = models.IntegerField()
    

    in my views I have:
    from django.http import HttpResponse
    import requests
    from requests import get
    from .models import latlon
    def home(request):
        ip = get('https://api.ipify.org').text
        url2 = format(ip)
        url ='http://api.ipstack.com/' +url2 +'?access_key=(my key is here)'
        data = requests.get(url)
        parsed_data = data.json()
    
        lat = parsed_data['latitude']
        long = parsed_data['longitude']
        region = parsed_data['region_name']
    
        html = "<html><body>It is now %s.<br />Your IP is: %s.<br />Your Latitude is %s.<br />Your Longitude is %s.<br />Your Region is %s.<br /></body></html>" % (now, url2, lat, long, region)
    
        return HttpResponse(html)
    

    when I run that, it prints out the lat, long and region.

    If though I try to save the latitude by using
    lat.save()
    

    (if thats the correct way to do it that is) I get an error saying " 'float' object has no attribute save'

    How to do I save the lat to the database? I've been googling and getting lots of info, but nothing I can use.


Comments

  • Registered Users, Registered Users 2 Posts: 6,271 ✭✭✭Buford T Justice


    I'm not a python expert, but you've declared lat as an entity in your model, but you're re-assigning it as a different type in your view it looks like to me.

    When you call save, it's trying to save on a float type and not an entity - hence the error


  • Moderators, Computer Games Moderators Posts: 4,282 Mod ✭✭✭✭deconduo


    maccored wrote: »
    Hi folks

    my heads wrecked with this. Im finishing a part-time hdip and Im trying to pull my final project together. Ive pulled latitude and longitude data from an API. I can extract the data I need and I can disply it on a page, using django. No issues with that.

    What I want to do is save the info to a data base. I've simplified it down to just saving the latitude info. As I say, I can print the info no problem. i have it stored in a variable called 'lat'.
    I set up a model with this info:
    from django.db import models
    
    class latlon(models.Model):
        lat = models.IntegerField()
    

    in my views I have:
    from django.http import HttpResponse
    import requests
    from requests import get
    from .models import latlon
    def home(request):
        ip = get('https://api.ipify.org').text
        url2 = format(ip)
        url ='http://api.ipstack.com/' +url2 +'?access_key=(my key is here)'
        data = requests.get(url)
        parsed_data = data.json()
    
        lat = parsed_data['latitude']
        long = parsed_data['longitude']
        region = parsed_data['region_name']
    
        html = "<html><body>It is now %s.<br />Your IP is: %s.<br />Your Latitude is %s.<br />Your Longitude is %s.<br />Your Region is %s.<br /></body></html>" % (now, url2, lat, long, region)
    
        return HttpResponse(html)
    

    when I run that, it prints out the lat, long and region.

    If though I try to save the latitude by using
    lat.save()
    

    (if thats the correct way to do it that is) I get an error saying " 'float' object has no attribute save'

    How to do I save the lat to the database? I've been googling and getting lots of info, but nothing I can use.

    You need to initiate an instance of a 'latlon' object, and save that. The following should do the trick:
        myLatlon = latlon(lat=parsed_data['latitude'])
        myLatlon.save()
    

    Assuming from the name that you want the longitude as well, you could continue with:
    class latlon(models.Model):
        lat = models.IntegerField()
        lon = models.IntegerField()
    
        myLatlon = latlon(lat=parsed_data['latitude'], lon=parsed_data['longitude'])
        myLatlon.save()
    
    


  • Registered Users, Registered Users 2 Posts: 10,794 ✭✭✭✭maccored


    thanks for that - got it working.

    final issue, I swear:

    I have this as a view:
    from django.shortcuts import render
    from get_co.models import latlon_getco
    from chargeInfo.models import destination
    def home(request):
    
    
        dest = destination.objects.get(id=1)
        destin = {'latitudeDest': dest.latitudeD,'longitudeDest': dest.longitudeD }
    
    
        orig= latlon_getco.objects.get(id=1)
        origin = {'latitudeOrigin': orig.lat,'longitudeOrigin': orig.lon}
    
    
    
        return render(request, 'display/home.html', destin,origin)
    

    Its basically two sets of co-ordinates. Thing is, only the first one listed appears on the page - not both. If I have it as 'return render(request, 'display/home.html', destin,origin)' then the destination info prints to the page, but the next one (origin) doesnt.

    If i use 'return render(request, 'display/home.html',origin, destin)' then the origin info prints to the page,but the next one (destin) doesnt

    How come both wont appear? Thank you for the help so far


  • Registered Users, Registered Users 2 Posts: 10,794 ✭✭✭✭maccored


    aha - the old 'post it on boards and then 5 mins later you'll figure it out' monster appears again :D:
    dest = destination.objects.get(id=1)
        orig= latlon_getco.objects.get(id=1)
        args = {'latitudeOrigin': orig.lat,'longitudeOrigin': orig.lon, 'latitudeDest': dest.latitudeD,'longitudeDest': dest.longitudeD}
    
    
        return render(request, 'display/home.html', args)
    


Advertisement