Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Add "shop" Table to Django Admin

Give Feedback

In this video we will add our shop database table to our admin view and load some initial records.

At this point you should have most of your app set up along with a superuser account for Django authentication.

What you will do now is register your shop model in the Admin interface. To do so you will subclass a GeoDjango class called OSMGeoAdmin and specifying the list of columns to display for the view.

Comments & Discussion

abairy on June 4, 2019

Hello When I try to migrate the data exported from overpass its giving me this error

File “C:\Users\13154\Anaconda3\lib\encodings\cp1252.py”, line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: ‘charmap’ codec can’t decode byte 0x9d in position 26427: character maps to <undefined>

snandw on June 22, 2019

please help I am getting the same error on a windows machine ,a quick reply would be appreciated i need to submit this as a project

Jackie Wilson RP Team on June 24, 2019

I am traveling with limited access. Do you mean to say your map data (location data in JSON) is causing the error?

drewewhite1 on Feb. 9, 2020

@abairy and @snandw, you may have solved your problem already. If not or for anyone who runs into this issue in the future, I was able to resolve the issue by specifying the encoding upon opening the file. So it looks something like this.

...

with open(str(jsonfile), encoding = "utf8") as datafile:
        objects = json.load(datafile)
        for obj....

Hope this helps!

Amr on Feb. 17, 2020

how to to use file.xlsx instead of file.json? and also how to add any type of file.

Mydhe on April 7, 2020

Just to add, the edit is done in your empty migration file for example mine is named 0002_auto_20200408_0157.py The edit is in this block;

with open(str**(jsonfile), encoding = "utf8") as **datafile:
    objects = json.load(datafile)
    for obj in objects['elements']:
        try:
            objType = obj['type']
            if objType == 'node':
                tags = obj['tags']
                name = tags.get('name','no-name')
                longitude = obj.get('lon', 0)
                latitude = obj.get('lat', 0)
                location = fromstr(f'POINT({longitude} {latitude})', srid=4326)
                Shop(name=name, location = location).save()
        except KeyError:
            pass

Thanks drewewhite1 for the pointer!

jacob6 on May 4, 2020

My Shops table is still empty…no error messages. This is my code:

# Generated by Django 3.0.5 on 2020-05-04 08:55

from django.db import migrations
import json
from django.contrib.gis.geos import fromstr
from pathlib import Path

DATA_FILENAME = 'export.json'

def load_data(apps, schema_editor):
    Shop = apps.get_model('nearbyshops', 'Shop')
    jsonfile = Path(__file__).parents[2] / DATA_FILENAME

    with open(str(jsonfile)) as datafile:
        objects = json.load(datafile)
        for obj in objects['elements']:
            try:
                objType = obj['type']
                if objType == 'node':
                    tags = obj['tags']
                    name = tags.get('name', 'no-name')
                    longitude = obj.get('lon', 0)
                    latitude = obj.get('lat', 0)
                    location = fromstr(
                        f'POINT({longitude} {latitude})', srid=4326
                    )
                    Shop(name=name, location=location).save()
            except KeyError:
                pass

class Migration(migrations.Migration):

    dependencies = [
        ('nearbyshops', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(load_data)
    ]

jacob6 on May 5, 2020

I’ve been over my code a few times now. I’m now getting the following error message:

TypeError: Shop() got an unexpected keyword argument 'location'

My code is as follows:

# Generated by Django 3.0.5 on 2020-05-04 10:00

from django.db import migrations
import json
from django.contrib.gis.geos import fromstr
from pathlib import Path

DATA_FILENAME = 'export.json'

def load_data(apps, schema_editor):
    Shop = apps.get_model('nearbyshops', 'Shop')
    jsonfile = Path(__file__).parents[2] / DATA_FILENAME

    with open(str(jsonfile)) as datafile:
        objects = json.load(datafile)
        for obj in objects['elements']:
            try:
                objType = obj['type']
                if objType == 'node':
                    tags = obj['tags']
                    name = tags.get('name', 'no-name')
                    longitude = obj.get('lon', 0)
                    latitude = obj.get('lat', 0)
                    location = fromstr(
                        f'POINT({longitude} {latitude})', srid=4326
                    )
                    Shop(name=name, location=location).save()
            except KeyError:
                pass

class Migration(migrations.Migration):

    dependencies = [
        ('nearbyshops', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(load_data)
    ]

As per the instructions in the video the export.json is on the same level as manage.py. I’ve been over this code a lot of times now, and I’ve tried googling, with no success…can anyone help out?

pmannion95 on July 23, 2020

Hi, I’m running into an odd error that I believe is related the map data but I’ve follow the instructions in the video for querying shops in Baltimore and downloading as raw OSM data.

django.contrib.gis.geos.error.GEOSException: Error encountered checking Geometry returned from GEOS C function "GEOSWKTReader_read_r".

My migration code is below, but otherwise I’m unsure of what may be the issue. I’ve retraced it, and redownloaded the export.json data to no avail.

My code is below: ``` # Generated by Django 3.0.8 on 2020-07-21 05:08

from django.db import migrations import json from django.contrib.gis.geos import fromstr from pathlib import Path

DATA_FILENAME = ‘export.json’

def load_data(apps, schema_editor): Shop = apps.get_model(‘nearbyshops’, ‘Shop’) jsonfile = Path(file).parents[2] / DATA_FILENAME

with open(str(jsonfile)) as datafile:
    objects = json.load(datafile)

    for obj in objects['elements']:
        try:
            objType = obj['type']
            if objType == 'node':
                tags = obj['tags']
                name = tags.get('name', 'no-name')
                longitude = obj.get('lon', 0)
                latitude = obj.get('lat', 0)
                location = fromstr(f'POINT({longitude} {latitude}', srid=4326)

                Shop(name=name, location=location).save()
        except KeyError:
            pass

class Migration(migrations.Migration):

dependencies = [
    ('nearbyshops', '0001_initial'),
]

operations = [
    migrations.RunPython(load_data)
]

```

Become a Member to join the conversation.