我有一个 actions.py 文件,它为我的一个模型的管理页面定义了自定义操作。它使用中间页面(如默认删除操作),因此具有相应的表单,该表单也在同一文件中声明。
出于某种原因,我已经放弃了数据库(开发),现在当我尝试运行 syncdb 时,它给了我以下错误:
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 是我的一个应用程序的名称。发生这种情况的原因是因为 actions.py 导入了 iiitd_clientinfo 模型——但显然它还不存在。目前,我通过排除/注释掉我的 actions.py 文件来使其工作。当然,必须有一个解决方法?
我尝试了以下方法:
- syncdb 之前的南迁移 - 同样的错误。
- 整理了 INSTALLED_APPS 顺序以确保它们处于拓扑顺序-相同的错误
在我的actions.py中使用了这个:
尝试:
clientObj = ClientInfo.objects.all()[:1].get()
除了 ClientInfo.DoesNotExist:
clientObj = None
编辑: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):
使固定
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')