Adding files to the django auto refresh

Thu 10 May 2012
By stu

Django runserver and the app django-autotest are both good as they'll restart when modifcations are made to files in the app they will restart, however not *all* files will trigger a restart.

Wanting to get some other files noticed, I added some in the __init__.py of my app, so it looked like this:

``` {: .brush:python} try:     from django.conf import settings

if settings.DEBUG:         import forms, util, tests except:     pass ```

This seemed to be working fine until I swapped to postgres and tried to do syncdb .. and reset, uh oh !

Postgres said (names changed to protect the innocent):

{: .brush:sql} 2012-05-10 09:47:57 BST STATEMENT: SELECT c.relname FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind IN ('r', 'v', '') AND n.nspname NOT IN ('pg_catalog', 'pg_toast') AND pg_catalog.pg_table_is_visible(c.oid) 2012-05-10 09:47:57 BST LOG: could not receive data from client: Connection reset by peer 2012-05-10 09:47:57 BST LOG: unexpected EOF on client connection 2012-05-10 09:49:14 BST ERROR: relation "sandwich_maker_filling" does not exist at character 72 2012-05-10 09:49:14 BST STATEMENT: SELECT "sandwich_maker_filling"."id", "sandwich_maker_filling"."name" FROM "sandwich_maker_filling" 2012-05-10 09:49:14 BST ERROR: current transaction is aborted, commands ignored until end of transaction block

And django wasn't too happy either:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/stu/.virtualenvs/the_app/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/home/stu/.virtualenvs/the_app/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/stu/.virtualenvs/the_app/local/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/stu/.virtualenvs/the_app/local/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
    output = self.handle(*args, **options)
  File "/home/stu/.virtualenvs/the_app/local/lib/python2.7/site-packages/django/core/management/base.py", line 371, in handle
    return self.handle_noargs(**options)
  File "/home/stu/.virtualenvs/the_app/local/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 60, in handle_noargs
    tables = connection.introspection.table_names()
  File "/home/stu/.virtualenvs/the_app/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 896, in table_names
    return self.get_table_list(cursor)
  File "/home/stu/.virtualenvs/the_app/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/introspection.py", line 33, in get_table_list
    AND pg_catalog.pg_table_is_visible(c.oid)""")
  File "/home/stu/.virtualenvs/the_app/local/lib/python2.7/site-packages/django/db/backends/util.py", line 40, in execute
    return self.cursor.execute(sql, params)
  File "/home/stu/.virtualenvs/the_app/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 52, in execute
    return self.cursor.execute(query, args)
django.db.utils.DatabaseError: current transaction is aborted, commands ignored until end of transaction block

In the end the solution was simple, just check for the args where we need the refresh to happen automatically:

```

Automatically reload when forms or tests changed

try: from django.conf import settings

if settings.DEBUG:
    import sys
    if sys.argv == ['manage.py', 'autotest'] or sys.argv == ['manage.py', 'runserver']:
            # Need to check params as otherwise this can break syncdb, reset and
            # Friends !!
            import forms, util, tests

except: pass ```

Hopefully this will be helpful to somebody with a similar error or that wants autotest or runserver to see more files.

This article was particularly helpful in debugging this:  debugging django syncdb