IntegrityError:(1048,“Column'field'不能为null”)即使null = True

时间:2017-05-02 18:18:58

标签: python django validation null

我在Django中遇到了完整性错误,我很难理解为什么。当我尝试保存我的formset时,我收到了错误

  

IntegrityError:(1048,“Column'field'不能为null”

但是在我的模型中我允许该字段为空,所以我不明白为什么会出现这个错误?

class Time(models.Model):
    description = models.CharField(max_length=150, null=True, blank=True)
    timesheet_id = models.ForeignKey(TimeSheet, null=True, related_name="time_set")
    field = models.CharField(max_length=10, null=True, blank=True, editable=False)
    def __unicode__ (self):
        return self.description or u''

这是我的追溯:

Internal Server Error: /app/2/timesheet/week/submit/
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/username/Documents/code/project/app/views.py", line 394, in my_view
    time_formset.save()
  File "/Library/Python/2.7/site-packages/django/forms/models.py", line 645, in save
    return self.save_existing_objects(commit) + self.save_new_objects(commit)
  File "/Library/Python/2.7/site-packages/django/forms/models.py", line 775, in save_new_objects
    self.new_objects.append(self.save_new(form, commit=commit))
  File "/Library/Python/2.7/site-packages/django/forms/models.py", line 913, in save_new
    obj.save()
  File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 700, in save
    force_update=force_update, update_fields=update_fields)
  File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 728, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 812, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 851, in _do_insert
    using=using, raw=raw)
  File "/Library/Python/2.7/site-packages/django/db/models/manager.py", line 122, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 1039, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py", line 1060, in execute_sql
    cursor.execute(sql, params)
  File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 117, in execute
    six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
  File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 112, in execute
    return self.cursor.execute(query, args)
  File "/Library/Python/2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "/Library/Python/2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
IntegrityError: (1048, "Column 'field' cannot be null")

如果我允许模型中的字段为null,为什么我会得到这个?

这是正在执行的视图,在运行timeformset.is_valid()后,“有效”是打印到控制台...所以我更加困惑......

def my_view(request, timesheet_id):
    timesheets = TimeSheet.objects.get(pk=timesheet_id)
    TimeFormSet = inlineformset_factory(TimeSheet, Time, form=TimeForm, extra=1, can_delete=True, formset=CustomInlineFormSet)
    if request.method == 'POST':
        time_formset = TimeFormSet(request.POST, instance=timesheets)
        if time_formset.is_valid(): 
            #This prints to the console         
            print "is validating"
            time_formset.save()
        else:
            print "not validating"
        return HttpResponseRedirect(reverse("app:next_view"))

帮助!

(我也刚从Git那里做了一次拉动并在另一台计算机上再次运行迁移,不确定这是否相关)

0 个答案:

没有答案