关于python:Django中的IntegrityError

IntegrityError in Django

我在其中一个表单中添加了许多字段,现在提交时得到一个整合性错误。准确的错误文本是

IntegrityError at /add_person/ hireterm_person.mail_lists may not be NULL

在我加入新的领域之前,它工作得很好。当我查看关于调试更改的发布数据时,我看到它正在被传递,所以我不知道在哪里发生了中断。

模型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from django.db import models
from django.forms import ModelForm
from django import forms
from django.contrib.auth.models import User

class Mailists(models.Model):
    name = models.CharField(blank=True, max_length=100)
    email = models.CharField(blank=True, max_length=100)

    def __unicode__(self):
        return u'%s' % (self.name)

class Person(models.Model):
    ROLE_CHOICES = (
        ('Mrkt', 'Marketing'),
        ('Fin/Off', 'Finance / Office'),
        ('Care', 'Care'),
        ('Sales', 'Sales'),
        )
    ROLE_TYPE = (
        ('Full', 'Full Time'),
        ('Part', 'Part Time'),
        ('Intern', 'Intern'),
        )

    first_name = models.CharField(blank=True, max_length=100)
    last_name = models.CharField(blank=True, max_length=100)
    date_added = models.DateField(auto_now_add=True)
    date_start = models.DateField(auto_now=False)
    role = models.CharField(max_length=100, default ="", choices = ROLE_CHOICES)
    manager = models.ForeignKey('self', limit_choices_to = {'is_manager': True}, null=True, blank=True)
    title = models.CharField(blank=True, max_length=100)
    role_type = models.CharField(max_length=100, default ="", choices = ROLE_TYPE)
    laptop_needed = models.BooleanField(default=True)
    phone_needed = models.BooleanField(default=True)
    desk_loco = models.CharField(blank=True, max_length=100)
    mail_lists = models.ManyToManyField(Mailists, blank=True)
    notes = models.CharField(blank=True, max_length=500)
    is_manager = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)

    def __unicode__(self):
        return u'%s %s' % (self.first_name, self.last_name)

class PersonForm(ModelForm):
    mail_lists = forms.ModelMultipleChoiceField(queryset=Mailists.objects.all(), widget=forms.CheckboxSelectMultiple(), required=False)
    class Meta:
        model = Person

编辑

我添加了mail_lists=request.post.getlist('mail_lists')。当我添加一个打印到这个列表中时,返回的列表是所有的复选框,放置的日志数据仍然是一个字符串,最后一个复选框被选中。

意见

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from hireterm.models import Person, Mailists, PersonForm
from django.shortcuts import get_object_or_404, render
from django.template import Context, loader
from django.http import HttpResponseRedirect, HttpResponse
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login
from django.contrib.auth import logout
from django.core.mail import send_mail

@login_required
def home(request):
    return render(request, 'hireterm/home.html')

def add_person(request):
    if request.method == 'POST':
        mail_lists = request.POST.getlist('mail_lists')
        person_form = PersonForm(request.POST)
        if person_form.is_valid():
            person_form.save()
            return HttpResponseRedirect('/new_hire') # Redirect after POST

    else:
        person_form = PersonForm() # An unbound form

    return render(request, 'hireterm/additem.html', {
        'form': person_form,
    })


在您的领域:

1
mail_lists = models.ManyToManyField(Mailists, blank=True)

您已将blank=True添加到字段中,但没有添加null=True。这意味着您的数据库期望该字段有一个值。所以如果没有,你会得到一个错误。

无效的

If True, Django will store empty values as NULL in the database. Default is False.
Note that empty string values will always get stored as empty strings, not as NULL. Only use null=True for non-string fields such as integers, booleans and dates. For both types of fields, you will also need to set blank=True if you wish to permit empty values in forms, as the null parameter only affects database storage (see blank).

空白的

If True, the field is allowed to be blank. Default is False.

Note that this is different than null. null is purely database-related, whereas blank is validation-related. If a field has blank=True, form validation will allow entry of an empty value. If a field has blank=False, the field will be required.

以下是一些其他解释:

  • 区分空=真,空=真
  • http://www.b-list.org/weblog/2006/jun/28/django-tips-difference-between-blank-and-null/