python - Django - 表 xxx 没有名为 xxx 的列

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

当我尝试将表单保存在 View 中时,我遇到了问题,然后我看到错误:表 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

最佳答案

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

关于python - Django - 表 xxx 没有名为 xxx 的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35429876/

相关文章:

python - 在 Django 模板中嵌入 Plotly 图表

django - 在Django REST Framework中找不到资源时如何返回404

python - 没有项目匹配给定的查询 : dropdown options in Django

python - NoReverseMatch at/accounts/password-reset/用于 Django 中的密码重置

django - 保存前更改表单实例 Django

python - Zeromq REP Node 只会获取第一条消息(Req,Rep模式)

python - 如何从字典中提取对到一个新的更小的字典中?

python - 将 python 中的循环替换为相当于 matlab 查找的循环

Django - 将表单集中的 BooleanField 显示为一组单选按钮

python - 杀死 Python 多处理池