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 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 "", line 10, in <module>
  File "/home/stu/.virtualenvs/the_app/local/lib/python2.7/site-packages/django/core/management/", line 443, in execute_from_command_line
  File "/home/stu/.virtualenvs/the_app/local/lib/python2.7/site-packages/django/core/management/", line 382, in execute
  File "/home/stu/.virtualenvs/the_app/local/lib/python2.7/site-packages/django/core/management/", 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/", line 232, in execute
    output = self.handle(*args, **options)
  File "/home/stu/.virtualenvs/the_app/local/lib/python2.7/site-packages/django/core/management/", 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/", 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/", 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/", 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/", 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/", 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 == ['', 'autotest'] or sys.argv == ['', '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