2016-11-10 88 views
0

所以我遵循DOCS,只是想確保我的理解正確。 https://docs.djangoproject.com/en/1.10/ref/contrib/gis/install/postgis/如何爲Django安裝PostGIS?

難道我只是創建一個名爲migrations.py與文件:

from django.contrib.postgres.operations import CreateExtension 
from django.db import migrations 

class Migration(migrations.Migration): 

    operations = [ 
     CreateExtension('postgis'), 
     ... 
    ] 

,並在我的項目目錄降了嗎?然後運行python manage.py makemigrations?

+0

'蟒蛇manage.py migrate'不'凡在我的項目目錄makemigrations' – dnit13

+0

?只是根? – david

回答

1

仍然是更好的方式IST通過使SQL查詢直接創建擴展:

CREATE EXTENSION postgis; 

之後,你只需要瀏覽到您的項目根(裏面有一個manage.py文件)並運行python manage.py migrate(因爲Django的1.9 - v.1.9第一次運行python manage.py makemigrations之前和之後python manage.py migrate)但是,如果你想用你的代碼,你有

將其添加到「models.py」。 這就是所謂的通過"python manage.py migrate"

文件所以你的models.py樣子:

from django.contrib.gis.db import models 
from django.contrib.postgres.operations import CreateExtension 
from django.db import migrations 

class Migration(migrations.Migration): 

    operations = [ 
     CreateExtension('postgis'), 
    ] 


class model1(models.Model): 
    geom = models.GeometryField(srid=4326,blank=True,null=True) 
    name = models.TextField(null=True)