从Django 1.8升级到1.9时的ProgrammingError

时间:2016-03-14 15:34:44

标签: django django-models django-autocomplete-light

我有多个应用程序的项目。其中一个应用程序具有联系人模型和供应商模型。供应商与Contact模型具有OneToOne关系。在Django 1.8.11中一切正常。当我尝试makemigrations或任何其他命令时,我收到以下错误。

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "D:\env\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "D:\env\lib\site-packages\django\core\management\__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:\env\lib\site-packages\django\core\management\base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "D:\env\lib\site-packages\django\core\management\base.py", line 398, in execute
    self.check()
  File "D:\env\lib\site-packages\django\core\management\base.py", line 426, in check
    include_deployment_checks=include_deployment_checks,
  File "D:\env\lib\site-packages\django\core\checks\registry.py", line 75, in run_checks
    new_errors = check(app_configs=app_configs)
  File "D:\env\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "D:\env\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    for pattern in resolver.url_patterns:
  File "D:\env\lib\site-packages\django\utils\functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "D:\env\lib\site-packages\django\core\urlresolvers.py", line 417, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "D:\env\lib\site-packages\django\utils\functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "D:\env\lib\site-packages\django\core\urlresolvers.py", line 410, in urlconf_module
    return import_module(self.urlconf_name)
  File "c:\python27\Lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "D:\mdjango\mdjango\urls.py", line 32, in <module>
    url(r'^parts?/', include('parts.urls')),
  File "D:\env\lib\site-packages\django\conf\urls\__init__.py", line 52, in include
    urlconf_module = import_module(urlconf_module)
  File "c:\python27\Lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "D:\mdjango\parts\urls.py", line 2, in <module>
    from parts import views, ajax_views
  File "D:\mdjango\parts\views.py", line 9, in <module>
    from parts import forms
  File "D:\mdjango\parts\forms.py", line 17, in <module>
    class PartForm(forms.ModelForm):
  File "D:\mdjango\parts\forms.py", line 20, in PartForm
    class Meta:
  File "D:\mdjango\parts\forms.py", line 26, in Meta
    "/contacts/create/?next=/parts/create", "Add Supplier")
  File "D:\mdjango\general\widgets.py", line 9, in __init__
    super(SelectWithButton, self).__init__(attrs, choices)
  File "D:\env\lib\site-packages\django\forms\widgets.py", line 514, in __init__
    self.choices = list(choices)
  File "D:\env\lib\site-packages\django\db\models\query.py", line 258, in __iter__
    self._fetch_all()
  File "D:\env\lib\site-packages\django\db\models\query.py", line 1074, in _fetch_all
    self._result_cache = list(self.iterator())
  File "D:\env\lib\site-packages\django\db\models\query.py", line 52, in __iter__
    results = compiler.execute_sql()
  File "D:\env\lib\site-packages\django\db\models\sql\compiler.py", line 848, in execute_sql
    cursor.execute(sql, params)
  File "D:\env\lib\site-packages\django\db\backends\utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "D:\env\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "D:\env\lib\site-packages\django\db\utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "D:\env\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "contacts_supplier" does not exist
LINE 1: ...tacts_supplier"."default_contract_target_id" FROM "contacts_...

基本上,我的Parts应用程序中有一个URL文件,它是从parts.views导入的,这是标准的Django程序。 parts.views文件是从parts.forms导入的,也是标准的Django过程。导致问题的形式是:

class PartForm(forms.ModelForm):
    class Meta:
        model = Part
        fields = ['supplier', 'number', 'name']
        widgets = {
            'supplier': SelectWithButton(None, Supplier.objects.all(),
                                        "/contacts/create/?next=/parts/create", "Add Supplier")
        }

这是SelectWithButton类:

from django.forms import Select
from django.template.loader import get_template


class SelectWithButton(Select):
    def __init__(self, attrs=None, choices=(), btn_url='', btn_txt=''):
        super(SelectWithButton, self).__init__(attrs, choices)
        self.btn_url = btn_url
        self.btn_txt = btn_txt

    def render(self, name, value, attrs=None, choices=()):
        context = {'href': self.btn_url, 'text': self.btn_txt,
                   'select_field': super(SelectWithButton, self).render(name, value, attrs, choices)}
        t = get_template("general\selectWithButton.html")
        return t.render(context)

错误似乎是它试图在迁移完成之前从Supplier获取所有对象。但是我正在尝试进行迁移,而且我遇到了这个错误。我确实读过Django 1.9现在检查manage.py命令中的URL文件,但似乎我没有做任何不寻常的事情。您可以在1.9.3页面上阅读更多内容:https://docs.djangoproject.com/en/1.9/releases/1.9.3/

当我注释掉对SelectWithButton的引用时,我在另一个有autocomplete_light.MultipleChoiceField的表单上得到了类似的错误。我究竟做错了什么?它在Django 1.8.11中运行良好。

这是另一个错误和形式:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "D:\env\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "D:\env\lib\site-packages\django\core\management\__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:\env\lib\site-packages\django\core\management\base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "D:\env\lib\site-packages\django\core\management\base.py", line 398, in execute
    self.check()
  File "D:\env\lib\site-packages\django\core\management\base.py", line 426, in check
    include_deployment_checks=include_deployment_checks,
  File "D:\env\lib\site-packages\django\core\checks\registry.py", line 75, in run_checks
    new_errors = check(app_configs=app_configs)
  File "D:\env\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "D:\env\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    for pattern in resolver.url_patterns:
  File "D:\env\lib\site-packages\django\utils\functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "D:\env\lib\site-packages\django\core\urlresolvers.py", line 417, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "D:\env\lib\site-packages\django\utils\functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "D:\env\lib\site-packages\django\core\urlresolvers.py", line 410, in urlconf_module
    return import_module(self.urlconf_name)
  File "c:\python27\Lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "D:\mdjango\mdjango\urls.py", line 33, in <module>
    url(r'^transactions?/', include('transactions.urls')),
  File "D:\env\lib\site-packages\django\conf\urls\__init__.py", line 52, in include
    urlconf_module = import_module(urlconf_module)
  File "c:\python27\Lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "D:\mdjango\transactions\urls.py", line 3, in <module>
    from views import *
  File "D:\mdjango\transactions\views.py", line 3, in <module>
    from forms import *
  File "D:\mdjango\transactions\forms.py", line 18, in <module>
    class InventoryTransactionForm(forms.ModelForm):
  File "D:\mdjango\transactions\forms.py", line 32, in InventoryTransactionForm
    required=False)
  File "D:\env\lib\site-packages\autocomplete_light\fields.py", line 74, in __init__
    self.get_choices(autocomplete, registry, widget)})
  File "D:\env\lib\site-packages\autocomplete_light\fields.py", line 82, in get_choices
    return ((a.choice_value(c), a.choice_label(c)) for c in a.choices)
  File "D:\env\lib\site-packages\django\db\models\query.py", line 258, in __iter__
    self._fetch_all()
  File "D:\env\lib\site-packages\django\db\models\query.py", line 1074, in _fetch_all
    self._result_cache = list(self.iterator())
  File "D:\env\lib\site-packages\django\db\models\query.py", line 52, in __iter__
    results = compiler.execute_sql()
  File "D:\env\lib\site-packages\django\db\models\sql\compiler.py", line 848, in execute_sql
    cursor.execute(sql, params)
  File "D:\env\lib\site-packages\django\db\backends\utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "D:\env\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "D:\env\lib\site-packages\django\db\utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "D:\env\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "parts_sns" does not exist
LINE 1: SELECT "parts_sns"."id", "parts_sns"."sn" FROM "parts_sns"

表格:

class InventoryTransactionForm(forms.ModelForm):
    parent_sn = autocomplete_light.ModelChoiceField(label="Parent SN", autocomplete='InventoryParentSNAutocomplete',
                                                    required=False)
    qty_in = forms.FloatField(label="In", required=False)
    qty_out = forms.FloatField(label="Out", required=False)
    notes = forms.CharField(required=False)
    part = autocomplete_light.ModelChoiceField(autocomplete="StandardAutocomplete")
    document = autocomplete_light.ModelChoiceField(autocomplete="OpenDocumentAutocomplete", required=False)

    # for Qty Out serial numbers
    sns = autocomplete_light.MultipleChoiceField(label="SNs", autocomplete='InventoryQtyOutSNsAutocomplete',
                                                 required=False)
    user = forms.ModelChoiceField(Employee.objects.filter(user__is_active=True))

    class Meta:
        model = Transaction
        fields = ['date', 'usage', 'notes', 'user', 'sns']

1 个答案:

答案 0 :(得分:3)

您的表单导致在数据库中创建供应商表之前评估float: right;查询集。

您可以通过在表单Supplier.objects.all()方法中设置小部件来解决此问题。

__init__