IntegrityError:(1048,“列'用户名'不能为空”)

时间:2017-08-03 15:58:04

标签: django

从众所周知的email类的User(AbstractUser)字段中,我得到了一点错误:IntegrityError: (1048, "Column 'username' cannot be null")

当我在

中取消注释以下方法时,会显示此错误
CustomerBaseForm(forms.Form) class : 

    def clean_email(self):
        email = self.form.cleaned_data.get('email')
        qs = User.objects.filter(email=email)

        if self.form.instance not in (None, ''):
            # This is from the update form, so we exclude the updated customer
            # from the search results
            qs = qs.exclude(pk=self.form.instance.pk)

        if qs.count():
            raise ValidationError(
                _('This email is already in use')
            )

我如何修复此方法以使其正常工作?如果它错过了问题中的某些内容,请告诉我。

编辑:

以下是该错误的追溯:

Traceback (most recent call last):
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 63, in __call__
    return self.application(environ, start_response)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 170, in __call__
    response = self.get_response(request)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 124, in get_response
    response = self._middleware_chain(request)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = response_for_exception(request, exc)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/core/handlers/exception.py", line 86, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/core/handlers/exception.py", line 128, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
    six.reraise(exc_type, exc_value, tb)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/core/handlers/exception.py", line 39, in inner
    response = get_response(request)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/utils/decorators.py", line 67, in _wrapper
    return bound_func(*args, **kwargs)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/utils/decorators.py", line 63, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "/home/jeremie/Projects/24-django/loanwolf/core/views/mixins.py", line 84, in dispatch
    return super(StaffRestrictedMixin, self).dispatch(request, *args, **kwargs)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/jeremie/Projects/24-django/loanwolf/core/views/generic.py", line 315, in post
    return super(FrontendMixedFormView, self).post(request, *args, **kwargs)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/views/generic/edit.py", line 183, in post
    return self.form_valid(form)
  File "/home/jeremie/Projects/24-django/loanwolf/customers/views.py", line 158, in form_valid
    self.customer.save()
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/contrib/auth/base_user.py", line 80, in save
    super(AbstractBaseUser, self).save(*args, **kwargs)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/db/models/base.py", line 796, in save
    force_update=force_update, update_fields=update_fields)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/db/models/base.py", line 824, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/db/models/base.py", line 908, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/db/models/base.py", line 947, in _do_insert
    using=using, raw=raw)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/db/models/query.py", line 1045, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1054, in execute_sql
    cursor.execute(sql, params)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/debug_toolbar/panels/sql/tracking.py", line 165, in execute
    return self._record(self.cursor.execute, sql, params)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/debug_toolbar/panels/sql/tracking.py", line 107, in _record
    return method(sql, params)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 115, in execute
    six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 110, in execute
    return self.cursor.execute(query, args)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
IntegrityError: (1048, "Column 'username' cannot be null")

与此追溯有关,我得到了:

def form_valid(self, form):
    g = form.cleaned_data.get
    self.customer = User(**{
        'first_name': g('first_name'),
        'last_name': g('last_name'),
        'username': g('email'),
        'email': g('email'),
    })

    # Saving the user now will trigger the signal to create his user &
    # customer profile
    self.customer.save()

2 个答案:

答案 0 :(得分:0)

错误很明显,您正在尝试根据我的理解保存User对象,但是您没有指定用户名,因此会引发错误。

从用户那里获取用户名。

答案 1 :(得分:0)

您忘记在clean_email方法的末尾返回值。这意味着form.cleaned_data.get('email')会在None方法中返回form_valid,当您尝试在数据库中保存IntegrityError时会得到username=None