Repository: django-extensions/django-extensions

Issue:
'reset_db' fails with PostGIS: 'PostGISCreation' object has no attribute 'template_postgis'
Hi!

I run `reset_db`, but I get the following error instead:

```
Traceback (most recent call last):
  File "./manage.py", line 12, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/alex/.virtualenvs/rof/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/Users/alex/.virtualenvs/rof/lib/python3.4/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/alex/.virtualenvs/rof/lib/python3.4/site-packages/django/core/management/base.py", line 393, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/alex/.virtualenvs/rof/lib/python3.4/site-packages/django/core/management/base.py", line 444, in execute
    output = self.handle(*args, **options)
  File "/Users/alex/.virtualenvs/rof/lib/python3.4/site-packages/django_extensions/management/commands/reset_db.py", line 148, in handle
    postgis_template = PostGISCreation(connection).template_postgis
AttributeError: 'PostGISCreation' object has no attribute 'template_postgis'
```

Please, tell me if there is some workaround for this.

DB connection URL:

```
DATABASE_URL=postgis://USER_NAME:USER_PASSWORD@127.0.0.1:5432/DB_NAME
```

Used versions:

| Target | Version |
| --- | --- |
| Debug Toolbar | 2 |
| Django | 1.8.3 |
| Django_Extensions | 1.5.5 |
| Grappelli | 2.6.5 |
| Python | 3.4.3 |
| Rest_Framework | 3.1.3 |
| Rosetta | 0.7.6 |
| PostgreSQL | 9.4.4 on x86_64-apple-darwin14.3.0, compiled by Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn), 64-bit |
| PostGIS | "2.1.7 r13414" GEOS="3.4.2-CAPI-1.8.2 r3921" PROJ="Rel. 4.9.1, 04 March 2015" GDAL="GDAL 1.11.2, released 2015/02/10" LIBXML="2.9.2" LIBJSON="UNKNOWN" TOPOLOGY RASTER |

Thanks!



Discussion:
Sounds like the database `template_postgis` does not exists.

I would assume this is a standard database that is created for `postgis` similarly to the default `template0` and `template1` databases ?

Either this is not the case and we need a patch to fix that or if `template_postgis` is supposed to exist we need a patch with a good error message instead of an exception.

Hopefully somebody with `postgis` experience can pick this up. PR's are welcome as always ! :)

Hi!

`template_postgis` actually exists and raw `crearedb` works really fine:

`createdb -T template_postgis -O rof_stats rof_stats -U postgres;`

After this `manage.py migrate` works fine as well. And in admin panel I can see and use GIS fields, e.g. Point.

So, everything works, except `reset_db` and it's a bit strange.

Hello @oblalex 
got any news with this issue ?

Hi, @artscoop 
Actually not. However, I can see this issue was given a label `need patch`.

I found out that the entire block of code in reset_db.py containing the bug can be avoided in my postgis configuration. That is, I'm close to sure that the code can be put in a (try/except AttributeError:pass) block without harm (because it solved my issue)

seems this is fixed in #754 , works for django 1.8 too, can it be merged and released pls?


Resolving patch:
diff --git a/django_extensions/management/commands/reset_db.py b/django_extensions/management/commands/reset_db.py
--- a/django_extensions/management/commands/reset_db.py
+++ b/django_extensions/management/commands/reset_db.py
@@ -157,10 +157,14 @@ def handle(self, *args, **options):
                 create_query += " WITH OWNER = \"%s\" " % owner
             create_query += " ENCODING = 'UTF8'"
 
-            if engine == 'postgis':
-                # fetch postgis template name if it exists
-                from django.contrib.gis.db.backends.postgis.creation import PostGISCreation
-                postgis_template = PostGISCreation(connection).template_postgis
+            if engine == 'postgis' and django.VERSION < (1, 9):
+                # For PostGIS 1.5, fetch template name if it exists
+                if django.VERSION < (1, 8):
+                    from django.contrib.gis.db.backends.postgis.creation import PostGISCreation
+                    postgis_template = PostGISCreation(connection).template_postgis
+                else:
+                    from django.contrib.gis.db.backends.postgis.base import DatabaseWrapper
+                    postgis_template = DatabaseWrapper(dbinfo).template_postgis
                 if postgis_template is not None:
                     create_query += ' TEMPLATE = %s' % postgis_template
 


Test patch: