[英]IntegrityError in Django
我在其中一个表单中添加了 A Many2Many 字段,现在我在提交时收到 IntegrityError 。 确切的错误文本是
/add_person/hiringterm_person.mail_lists 处的 IntegrityError 可能不是 NULL
在我添加新字段之前,它运行良好。 当我查看有关调试更改的 POST 数据时,我看到它正在传递,因此我不知道中断发生在哪里。
楷模
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')。 当我为此添加打印时,返回的列表是所有复选框,POST 数据仍然是单个字符串,最后一个框被选中。
观看次数
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,
})
在你的领域:
mail_lists = models.ManyToManyField(Mailists, blank=True)
您已将blank=True
添加到该字段,但未添加null=True
。 这意味着您的数据库期望该字段具有值。 所以当它没有时,你会得到一个错误。
如果为 True,Django 将在数据库中将空值存储为 NULL。 默认值为假。 请注意,空字符串值将始终存储为空字符串,而不是 NULL。 仅对非字符串字段(例如整数、布尔值和日期)使用 null=True。 对于这两种类型的字段,如果您希望在表单中允许空值,您还需要设置 blank=True,因为 null 参数仅影响数据库存储(参见空白)。
如果为 True,则该字段可以为空。 默认值为假。
请注意,这与 null 不同。 null 纯粹与数据库相关,而空白与验证相关。 如果一个字段有空白=真,表单验证将允许输入一个空值。 如果某个字段有空白 = False,则该字段将是必需的。
以下是其他一些解释:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.