3

I have some existing tables (shapefiles) and I want to connect each table with a django model. I imported the tables with shp2pgsql tool:

shp2pgsql -I -s 2100 -W 'iso-8859-7' world.shp public.world | psql -h localhost -d django_db -U django_user

Then I ran:

python manage.py inspectdb

and I copied the model of that table in my models.py :

class World(models.Model):
    aa = models.IntegerField()
    yparith = models.CharField(max_length=254)
    name = models.CharField(max_length=100)
    dep = models.CharField(max_length=50)
    arfakellou = models.CharField(max_length=50)
    sxolia = models.CharField(max_length=250)
    parakatath = models.CharField(max_length=250)
    yp = models.CharField(max_length=20)
    geom = models.MultiLineStringField(srid=2100)
    objects = models.GeoManager()

    class Meta:
        managed = True
        db_table = 'geoloc_world'

    def __str__(self):
        return self.name

    def __unicode__(self):
        return self.name

After that I ran:

python manage.py makemigrations
python manage.py migrate

Then when in django shell I type:

World.objects.all()

the output is an empty list.

I tried to use LayerMapping data import utility but it raises the following error:

 LayerMapError: Could not retrieve geometry from feature.

My load.py:

import os
from django.contrib.gis.utils import LayerMapping
from .models import World

geoloc_mapping = {
    'aa' : 'aa',
    'yparith' : 'YpArith',
    'name' : 'Name',
    'dep' : 'Dep',
    'arfakellou' : 'ArFakellou',
    'sxolia' : 'Sxolia',
    'parakatath' : 'Parakatath',
    'yp' : 'Yp',
    'geom' : 'MULTILINESTRING',
 }

geoloc_shp = os.path.abspath(
os.path.join(os.path.dirname(__file__), 'data',                                                                                'world.shp'),
)

def run(verbose=True):
     lm = LayerMapping(
     World, geoloc_shp,
     geoloc_mapping, transform=False,
     encoding='iso-8859-7',
     )
lm.save(strict=True, verbose=verbose)

Any suggestions ?

2
  • what's your database connection look like in settings.py?
    – Reiion
    Commented Oct 10, 2017 at 23:57
  • Thank you for your comment @Reiion. Finally I managed to solved the problem. The process that I describe above is right. My list was empty after python manage.py make migrations, python manage.py migrate because I hadn't copied right the model that python manage.py inspectdb command exported and the model in my models.py. I apologize for my delay to inform that I solved the problem.
    – Capdi
    Commented Oct 12, 2017 at 7:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.