I have an actions.py file which defines custom actions for the admin page for one of my models. It uses an intermediary page (like the default delete action) and hence has a corresponding form which is also declared in the same file.
For some reason, I had drop by database (development) and now when I try to run syncdb, it gives me the following error:
Traceback (most recent call last):
File "/home/vinayak/pyCharm/helpers/pycharm/django_manage.py", line 23, in <module>
run_module(manage_file, None, '__main__', True)
File "/usr/lib/python2.7/runpy.py", line 176, in run_module
fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 82, in _run_module_code
mod_name, mod_fname, mod_loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/home/vinayak/zenatix/customuser/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 284, in execute
self.validate()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 310, in validate
num_errors = get_validation_errors(s, app)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/validation.py", line 34, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 196, in get_app_errors
self._populate()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 75, in _populate
self.load_app(app_name, True)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 99, in load_app
models = import_module('%s.models' % app_name)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 40, in import_module
__import__(name)
File "/usr/local/lib/python2.7/dist-packages/debug_toolbar/models.py", line 63, in <module>
patch_root_urlconf()
File "/usr/local/lib/python2.7/dist-packages/debug_toolbar/models.py", line 51, in patch_root_urlconf
reverse('djdt:render_panel')
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 480, in reverse
app_list = resolver.app_dict[ns]
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 310, in app_dict
self._populate()
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 262, in _populate
for pattern in reversed(self.url_patterns):
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 346, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 341, in urlconf_module
self._urlconf_module = import_module(self.urlconf_name)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 40, in import_module
__import__(name)
File "/home/vinayak/zenatix/customuser/customuser/urls.py", line 6, in <module>
admin.autodiscover()
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/__init__.py", line 29, in autodiscover
import_module('%s.admin' % app)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 40, in import_module
__import__(name)
File "/home/vinayak/zenatix/customuser/iiitd/admin.py", line 3, in <module>
from actions import grant_read_permission
File "/home/vinayak/zenatix/customuser/iiitd/actions.py", line 13, in <module>
class SelectUserForm(forms.Form):
File "/home/vinayak/zenatix/customuser/iiitd/actions.py", line 16, in SelectUserForm
clientObj = ClientInfo.objects.all()[:1].get()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 301, in get
num = len(clone)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 77, in __len__
self._fetch_all()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 854, in _fetch_all
self._result_cache = list(self.iterator())
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 220, in iterator
for row in compiler.results_iter():
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 710, in results_iter
for rows in self.execute_sql(MULTI):
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 781, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py", line 69, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 99, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "iiitd_clientinfo" does not exist
LINE 1: ...ientinfo"."id", "iiitd_clientinfo"."corp_id" FROM "iiitd_cli...
iiitd is the name of one of my apps. The reason this is happening is because actions.py imports the iiitd_clientinfo model - but that doesn't exist yet, obviously. Currently, I make it work by excluding/commenting out my actions.py file. Surely, there has to be a work around?
I have tried the following:
Used this in my actions.py:
try:
clientObj = ClientInfo.objects.all()[:1].get()
except ClientInfo.DoesNotExist:
clientObj = None
EDIT: Relevent part of actions.py:
from django import forms, template
from django.contrib import admin
from django.contrib.admin import helpers
from django.shortcuts import render_to_response
from django.utils.encoding import force_unicode
from guardian.shortcuts import assign_perm
from django.utils.translation import ugettext as _
from models import ClientInfo
from customauth.models import ZenatixUser
class SelectUserForm(forms.Form):
_selected_action = forms.CharField(widget=forms.MultipleHiddenInput)
try:
clientObj = ClientInfo.objects.all()[:1].get()
except ClientInfo.DoesNotExist:
clientObj = None
if clientObj:
client = clientObj.corp
client_name = client.shortName
client_id = client.cID
userList = ZenatixUser.objects.filter(corp__cID=client_id)
user = forms.ModelMultipleChoiceField(userList, label=client_name + ' users ')
def grant_read_permission(modeladmin, request, queryset):
Fix
class SelectUserForm(forms.Form):
_selected_action = forms.CharField(widget=forms.MultipleHiddenInput)
def __init__(self, initial, *args, **kwargs):
super(SelectUserForm, self).__init__(*args, **kwargs)
try:
clientObj = ClientInfo.objects.all()[:1].get()
client = clientObj.corp
client_name = client.shortName
client_id = client.cID
userList = ZenatixUser.objects.filter(corp__cID=client_id)
#user = forms.ModelMultipleChoiceField(userList, label=client_name + ' users ')
self.fields['user'] = forms.ModelMultipleChoiceField(userList, label=client_name + ' users ')
self._selected_action = forms.CharField(widget=forms.MultipleHiddenInput)
except ClientInfo.DoesNotExist:
raise Exception('Please add a client info object to the client')
It's mostly a wild guess until you post the source for your actions.py
module, but from the traceback I assume line 16 is at the top level of your SelectUserForm
class body. If so, this statement will be executed each time the module is imported, which is a BadThing for many reasons. You just discovered one of these reasons, congrats. Another one is that this will be executed once per process, and only once per process, which leads to to strange bugs as soon as you're in production (stale state, incoherent state from one process to another, etc).
The cure here is to move this statement within a method of your form.