簡體   English   中英

刪除遷移和makemigrations之后django.db.utils.OperationalError

[英]django.db.utils.OperationalError after removing migrations & makemigrations

我已經刪除了項目中的所有遷移,刪除了數據庫,並嘗試使用manage.py makemigrations命令重新創建遷移。 但是我得到了錯誤:

Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/helpers/pycharm/django_manage.py", line 41, in <module>
    run_module(manage_file, None, '__main__', True)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 176, in run_module
    fname, loader, pkg_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 82, in _run_module_code
    mod_name, mod_fname, mod_loader, pkg_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/Users/rankor/src/python/web/Auction/Auction/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
    utility.execute()
  File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/core/management/__init__.py", line 342, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/core/management/base.py", line 398, in execute
    self.check()
  File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/core/management/base.py", line 426, in check
    include_deployment_checks=include_deployment_checks,
  File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/core/checks/urls.py", line 10, in check_url_config
    return check_resolver(resolver)
  File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/core/checks/urls.py", line 19, in check_resolver
    for pattern in resolver.url_patterns:
  File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
    return import_module(self.urlconf_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/rankor/src/python/web/Auction/Auction/Auction/urls.py", line 14, in <module>
    url(r'^orders/', include('orders.urls', namespace='orders')),
  File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 52, in include
    urlconf_module = import_module(urlconf_module)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/rankor/src/python/web/Auction/Auction/orders/urls.py", line 3, in <module>
    from orders.views import CreateOrderView, OrdersView, OrderView
  File "/Users/rankor/src/python/web/Auction/Auction/orders/views.py", line 7, in <module>
    from orders.forms import OrderForm
  File "/Users/rankor/src/python/web/Auction/Auction/orders/forms.py", line 47, in <module>
    class OrderForm(ModelForm):
  File "/Users/rankor/src/python/web/Auction/Auction/orders/forms.py", line 60, in OrderForm
    widget=forms.Select, queryset=OrderCategory.objects.filter(parent__isnull=False).order_by('ordering'))
  File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/forms/models.py", line 1142, in __init__
    self.queryset = queryset
  File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/forms/models.py", line 1168, in _set_queryset
    self.widget.choices = self.choices
  File "/Users/rankor/src/python/web/Auction/Auction/orders/forms.py", line 16, in _get_choices
    if not self.queryset:
  File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/db/models/query.py", line 266, in __nonzero__
    return type(self).__bool__(self)
  File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/db/models/query.py", line 262, in __bool__
    self._fetch_all()
  File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/db/models/query.py", line 1074, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/db/models/query.py", line 52, in __iter__
    results = compiler.execute_sql()
  File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 852, in execute_sql
    cursor.execute(sql, params)
  File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Users/rankor/src/python/web/Auction/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 323, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: orders_ordercategory

訂單:

class OrderForm(ModelForm):
    """
    Order form
    """

    class Meta:
        model = Order
        fields = ('city', 'category', 'title', 'min_price', 'max_price', 'description', 'delivery_type', 'valid_to')

    city = forms.ModelChoiceField(
            widget=forms.Select, queryset=City.objects.filter(country__code='RU').order_by('name'))

    category = CategoryChoiceField(
            widget=forms.Select, queryset=OrderCategory.objects.filter(parent__isnull=False).order_by('ordering'))

    valid_to = forms.DateField(widget=SelectDateWidget)

class GroupedModelChoiceField(forms.ModelChoiceField):
    def optgroup_from_instance(self, obj):
        return ""

    def __choice_from_instance__(self, obj):
        return obj.id, self.label_from_instance(obj)

    def _get_choices(self):
        if not self.queryset:
            return []

        all_choices = []
        if self.empty_label:
            current_optgroup = ""
            current_optgroup_choices = [("", self.empty_label)]
        else:
            current_optgroup = self.optgroup_from_instance(self.queryset[0])
            current_optgroup_choices = []

        for item in self.queryset:
            optgroup_from_instance = self.optgroup_from_instance(item)
            if current_optgroup != optgroup_from_instance:
                all_choices.append((current_optgroup, current_optgroup_choices))
                current_optgroup_choices = []
                current_optgroup = optgroup_from_instance
            current_optgroup_choices.append(self.__choice_from_instance__(item))

        all_choices.append((current_optgroup, current_optgroup_choices))

        return all_choices

    choices = property(_get_choices, forms.ChoiceField._set_choices)


class CategoryChoiceField(GroupedModelChoiceField):
    def optgroup_from_instance(self, obj):
        return obj.parent.title

為什么以及如何解決?

PS Django 1.9,python 2.7,OSX。

查看堆棧跟蹤。

在預熱過程中,Django會導入所有模型和URL。

您應該將queryset初始化移到OrderForm的init方法。

有關更一般的說明,請回答洛倫佐。 所有情況下,您都希望將字段查詢集初始化移動到表單的init方法或將它們移動到視圖。 為什么? 實際上,啟動Django時,它可以導入一些表單,構建表單類,並且字段類屬性也會發生同樣的情況。 其結果是,在查詢集是獲取直接, 一勞永逸 ,輸入您的形式和時保持不變 ,只要你不重裝的服務器,所以如果你將數據添加到您的OrderCategory它永遠不會出現在你的選擇部件。

“解決方法”是將查詢集初始化移動到表單的init方法。

在您的情況下:

class OrderForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(OrderForm, self).__init__(*args, **kwargs)
        self.fields['city'].queryset = City.objects.filter(country__code='RU').order_by('name')
        self.fields['category'].queryset = OrderCategory.objects.filter(parent__isnull=False).order_by('ordering')
    class Meta:
       #...
    # ... fields definition

有關相關Django文檔的信息,請參見處理關系的字段

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM