Question

I have a pretty brief question: I already have a project with a blog app set up using PostgreSQL for my database back-end. I'm about to start experimenting with GeoDjango and PostGIS. In order to create a database to use GeoDjango, the database has to use a spatial_template. I really don't want to have to manage two projects and do weird things to get the two projects to work together. If it is possible, I'd like to simply install PostGIS, and start a new app within the same project. Then, I'm hoping that I'll be able to just start a new database with a spatial template to run my spatial data.

My question is this: Will I still be able to use my non-spatial blog models (or any other models, for that matter) with a PostGIS database using a spatial_template?

I don't want to go through all of the installation of GeoDjango dependencies just to find out it won't work, that's why I am asking first.

Looking forward to your feedback.

Was it helpful?

Solution

If you have PostgreSQL 9.1 or later, you can add PostGIS as an extension to an existing database:

CREATE EXTENSION postgis;

And if you find it isn't what you want, then remove it:

DROP EXTENSION postgis;

OTHER TIPS

Yes it can be done. I created two models, one with points, and another with non-spatial related data. Used template_postgis. Here's some code:

DATABASES = {
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        # 'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'pccs',
        'USER': 'xxxxx',
        'PASSWORD': 'xxxxx',
        'HOST': 'localhost', 
        'PORT': '5432', # Set to empty string for default.
}

}

from django.contrib.gis.db import models

class Station(models.Model):
    station_num = models.IntegerField("Station Number", max_length=4, primary_key=True)
    station_name = models.CharField("Station Name", max_length=50)
    station_type = models.CharField("Station Type", max_length=20, choices=TYPE_CHOICES)
    geom = models.PointField(srid=4326)
    objects = models.GeoManager()

def __unicode__(self):
    return unicode(self.station_name)

class Meta:
    verbose_name_plural = 'Monitoring Stations'


class Nutrient(models.Model):
    station_num = models.ForeignKey(Station)
    sample_date = models.DateField(editable=True)
    #sample_depth = models.IntegerField("Sample Depth", max_length=4, default="1")
    a = models.DecimalField("Temperature", max_digits=10, decimal_places=4)
    ...
    o = models.DecimalField("ph", max_digits=10, decimal_places=4, blank=True, null=True)

def __unicode__(self):
    return unicode(self.station_num_id)


class Meta:
    verbose_name_plural = 'Water Sample Data'

Hope this is what you were looking for

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top