Django IntegrityError(1048,“Column'network_id'不能为空”)

时间:2013-04-01 21:05:05

标签: django django-forms

好的,所以我为此搜索了高低,而且两个文档,以及Google上的几乎所有页面都告诉我,我的代码应该是合法的。

让我先来看看models.py:

class Network(models.Model):
    title = models.CharField(max_length=50)
    description = models.CharField(max_length=500)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    def __unicode__(self):
       return self.id

class NetworkForm(ModelForm):
    class Meta:
        model = Network

class Range(models.Model):
    network = models.ForeignKey(Network)
    range_start = models.GenericIPAddressField(unpack_ipv4=True)
    range_end = models.GenericIPAddressField(unpack_ipv4=True)
    range_title = models.CharField(max_length=50)
    range_description = models.CharField(max_length=300)

    def __unicode__(self):
        return self.range_title

class RangeForm(ModelForm):
    class Meta:
        model = Range
        exclude = ('network',)

然后我的views.py:

@login_required(login_url='/login/')
def add_network(request):

    if request.method == "POST":
        net_form = NetworkForm(request.POST)
        range_formset = RangeForm(request.POST)
        if net_form.is_valid and range_formset.is_valid:
            net_form.save(commit=False)
            range_formset.network = net_form
            range_formset.save()
            net_form.save()

        return HttpResponseRedirect('/')
    else:
        net_form = NetworkForm()
        range_form = RangeForm()
        return render_to_response('networks/add.html', {
            "net_form": net_form,
            "range_form": range_form,
        }, context_instance=RequestContext(request))

我的模板:

{% extends 'base.html' %}

{% block title %}Add Network{% endblock %}

{% block content %}
<div class="row-fluid">
    <div class="span7 offset2 well">
        <h3>Add network:</h3>
        <hr />
        <form class="form-horizontal" method="post" action="">
            {% csrf_token %}
            {{ net_form }}
            <h4>Add a range to the network:</h4>
            <hr />
            <div class="form-inline">
                {{ range_form }}
            </div>
            <hr />
            <button type="submit" class="btn btn-primary btn-large">Add network</button>
        </form>
    </div>
</div>

{%endblock%}

现在,大多数情况都是显而易见的,我在这里失踪了,只是没有做出最后的连接。如果有人能够启发我会很棒。

PS:如果有人想通过查看MySQL Workbench,MySQL Commandline客户端以及manage.py dbshel​​l和inspectdb来验证我的模型,则所有id列都存在。我甚至删除了整个数据库并使用manage.py重新创建它。

现在我的齿轮磨了大约一个星期,所以有时间寻求帮助。如果有人能帮助我解决这个问题,那就太棒了!

这是追溯:

Environment:


Request Method: POST
Request URL: http://localhost:8000/network/add/

Django Version: 1.5
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django_nose',
 'networks')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in   get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view
  25.                 return view_func(request, *args, **kwargs)
File "/home/alexander/PycharmProjects/NOC/networks/views.py" in add_network
  29.             range_form.save()
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in save
  370.                              fail_message, commit, construct=False)
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in save_instance
  87.         instance.save()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save
  546.                        force_update=force_update, update_fields=update_fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save_base
  650.                 result = manager._insert([self], fields=fields,    return_id=update_pk, using=using, raw=raw)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py" in _insert
  215.         return insert_query(self.model, objs, fields, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in insert_query
  1673.     return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py" in execute_sql
  937.             cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py" in execute
  41.             return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py" in execute
  127.                 six.reraise(utils.IntegrityError,   utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py" in execute
  120.             return self.cursor.execute(query, args)
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py" in execute
  174.             self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py" in defaulterrorhandler
  36.     raise errorclass, errorvalue

Exception Type: IntegrityError at /network/add/
Exception Value: (1048, "Column 'network_id' cannot be null")

1 个答案:

答案 0 :(得分:3)

您至少有四个错误。

首先,您必须实际调用 is_valid:它是一种方法,而不是属性。

其次,form.save(commit=False) 返回模型实例。它不会将表单对象神奇地转换为实例。您需要将其分配给变量,然后在该变量上设置额外属性。

第三,你在错误的对象上做commit=False - 你应该在你想要修改的那个上做。

第四,除非表格有效,否则不要重定向。

所以:

range_form = RangeForm(request.POST)
if net_form.is_valid() and range_form.is_valid():
    net_obj = net_form.save()
    range_obj = range_form.save(commit=False)
    range_obj.network = net_obj
    range_obj.save()
    return HttpResponseRedirect('/')
else:

    net_form = NetworkForm()
    range_form = RangeForm()

return render_to_response('networks/add.html', {
    "net_form": net_form,
    "range_form": range_form,
}, context_instance=RequestContext(request))

(并且当它们不是时,不要调用formsets,它们只是表单。除了当然你实际上应该使用范围表单的内联表单集,但这是一个单独的问题。)