Django中的IntegrityError [英] IntegrityError in Django

查看:52
本文介绍了Django中的IntegrityError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一种表单中添加了Many2Many字段,现在在提交时收到IntegrityError.确切的错误文本是

I added A Many2Many field in one of my forms and now I am getting an IntegrityError on submit. The exact error text is

/p_add/person/hireterm_person.mail_lists中的

IntegrityError可能不为空

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

在我添加新字段之前,它运行良好.当我查看有关调试更改的POST数据时,看到它已通过,因此我不知道中断发生在何处.

It worked fine until I added the new field. When I look at the POST data on the debug change, I see it being passed, so I don't know where the break occurs.

模型

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数据仍然是单个字符串,最后一个选中的框.

I added mail_lists = request.POST.getlist('mail_lists'). When I add a print to this, the list returned is all of the check boxes, put the POST data is still a single string, the last box checked.

观看次数

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 .这意味着您的数据库期望该字段具有值.因此,如果没有,您会得到一个错误.

you have added blank=True to the field, but not null=True. This means that your DB is expecting that field to have a value. So when it doesn't you get an error.

为空

null

如果为True,则Django将在数据库中将空值存储为NULL.默认值为False.请注意,空字符串值将始终存储为空字符串,而不是NULL.仅将null = True用于非字符串字段,例如整数,布尔值和日期.对于两种类型的字段,如果您希望在表单中允许使用空值,则还需要设置blank = True,因为null参数仅影响数据库存储(请参见空白).

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).

空白

blank

如果为True,则该字段允许为空白.默认值为False.

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

请注意,这不同于null.null纯粹与数据库有关,而空白则与验证有关.如果字段的空白为True,则表单验证将允许输入一个空值.如果一个字段的空白值为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.

以下是其他一些解释:

这篇关于Django中的IntegrityError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆