Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

saving json data to a database in django

  • 16-11-2018 3:36pm
    #1
    Registered Users, Registered Users 2 Posts: 10,748 ✭✭✭✭


    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,262 ✭✭✭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,748 ✭✭✭✭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,748 ✭✭✭✭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