Django - 表xxx没有名为xxx的列

时间:2016-02-16 10:23:38

标签: python django django-forms django-templates django-views

当我尝试将其保存在视图中时,我的表单出现问题然后我看到错误:表core_order没有名为responsible_group_id的列。我做了迁移,冲洗等。但这不能解决我的问题。

Forms.py:

class CreateForm(forms.ModelForm):

    class Meta:
        model = Order
        fields = ('responsible_group', 'responsible_user')

    def __init__(self, request_client_id, *args, **kwargs):
        super(CreateForm, self).__init__(*args, **kwargs)
        self.fields['responsible_group'].choices = BLANK_CHOICE + tuple(
            Group.objects.all().values_list('id', 'name').exclude(id=1)) 
        self.fields['responsible_user'].choices = BLANK_CHOICE

        self.fields['responsible_group'].widget.attrs.update({
            'type': 'text',
            'class': 'form-control input-block-level',
        })
        self.fields['responsible_user'].widget.attrs.update({
            'type': 'text',
            'class': 'form-control input-block-level',
        })

models.py:

class Order(models.Model):
    responsible_group = models.ForeignKey(Group)
    responsible_user = models.ForeignKey(User, blank=True, null=True, related_name='+')
    created_date = models.DateTimeField(auto_now_add=True, editable=False)
    finished_date = models.DateTimeField(null=True, blank=True)
    modified_date = models.DateTimeField(auto_now=True)
    (...)

views.py:

def order_create(request, request_client_id):

        if request.method == 'POST':
            form_order = CreateForm(request_client_id, request.POST)
            if form_order.is_valid():
                obj = form_order.save(commit=False)

                (...)

                obj.responsible_group = form_order.cleaned_data["responsible_group"]
                obj.responsible_user = form_order.cleaned_data["responsible_user"]

                obj.save()

                # Redirect to Client profile
                return HttpResponse('')

我的追溯:

Environment:

Request Method: POST
Request URL: http://127.0.0.1:8000/panel/order/create/4/

Django Version: 1.8.8
Python Version: 3.5.1
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'core',
 'api',
 'client',
 'registration',
 'avatar',
 'filer',
 'mptt',
 'easy_thumbnails',
 'reversion')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')

    File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\core\handlers\base.py" in get_response
      132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
      22.                 return view_func(request, *args, **kwargs)
    File "C:\Users\loc\PycharmProjects\CRM\core\views.py" in order_create
      537.                 obj.save()
    File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\models\base.py" in save
      734.                        force_update=force_update, update_fields=update_fields)
    File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\models\base.py" in save_base
      762.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
    File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\models\base.py" in _save_table
      846.             result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
    File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\models\base.py" in _do_insert
      885.                                using=using, raw=raw)
    File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\models\manager.py" in manager_method
      127.                 return getattr(self.get_queryset(), name)(*args, **kwargs)
    File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\models\query.py" in _insert
      920.         return query.get_compiler(using=using).execute_sql(return_id)
    File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
      974.                 cursor.execute(sql, params)
    File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\backends\utils.py" in execute
      79.             return super(CursorDebugWrapper, self).execute(sql, params)
    File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\backends\utils.py" in execute
      64.                 return self.cursor.execute(sql, params)
    File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\utils.py" in __exit__
      98.                 six.reraise(dj_exc_type, dj_exc_value, traceback)
    File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\utils\six.py" in reraise
      685.             raise value.with_traceback(tb)
    File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\backends\utils.py" in execute
      64.                 return self.cursor.execute(sql, params)
    File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\backends\sqlite3\base.py" in execute
      318.         return Database.Cursor.execute(self, query, params)

    Exception Type: OperationalError at /panel/order/create/4/
    Exception Value: table core_order has no column named responsible_group_id

1 个答案:

答案 0 :(得分:0)

尝试删除迁移文件,然后重新运行python manage.py makemigrationspython manage.py migrate