我正在构建电子病历软件。我的观点之一包含捕获患者基本信息的表格。
表格
from django import forms
from nesting.models import Identity_unique
class Identity_Form(forms.ModelForm):
NIS = forms.CharField(
widget=forms.TextInput(
attrs={
'placeholder': 'Enter NIS',
'class' : 'form-control'
}
)
)
First_Name = forms.CharField(
widget=forms.TextInput(
attrs={
'placeholder': 'Enter First Name',
'class' : 'form-control'
}
)
)
Last_Name = forms.CharField(
widget=forms.TextInput(
attrs={
'placeholder': 'Enter Last Name',
'class' : 'form-control'
}
)
)
Residence = forms.CharField(
widget=forms.TextInput(
attrs={
'placeholder': 'Enter Address',
'class' : 'form-control'
}
)
)
DOB = forms.CharField(
widget=forms.TextInput(
attrs={
'placeholder': 'Enter Date of Birth',
'class' : 'form-control'
}
)
)
class Meta:
model = Identity_unique
fields = ('NIS', 'First_Name', 'Last_Name', 'Residence', 'DOB',)
modelForm的数据模式
models.py
from django.db import models
from django.contrib.auth.models import User
from Identity import settings
import datetime
# Create your models here.
class Identity_unique(models.Model):
NIS = models.CharField(max_length = 200, primary_key = True)
user = models.ForeignKey(settings.AUTH_USER_MODEL)
Timestamp = models.DateTimeField(auto_now = True)
First_Name = models.CharField(max_length = 80, null = True, )
Last_Name = models.CharField(max_length = 80, null = True, )
Residence = models.CharField(max_length = 80, blank = True )
DOB = models.DateField(auto_now = False)
不幸的是,每次尝试将保存的数据提交到服务器时,我总是收到此错误:
IntegrityError at /nesting/
null value in column "date_of_birth" violates not-null constraint
DETAIL: Failing row contains (Q234934, 2, 2017-10-06 19:17:42.084063+00, Andre, James, nou.self@gmail.com, null, 1991-12-10).
我正在为数据库使用postgresql 9.6。我删除了migrations文件夹并多次迁移了模型,但是
null
和IntegrityError
仍然存在。我也一直在数据库中寻找数据库表Identity_unique,但找不到它。我这样做是为了删除具有空约束的列,因为我将生日字段从date_of_birth = models.DateField(max_length = 100, default = 0)
更改为DOB = models.DateField(auto_now = False).
如您所见,在错误中有一个带有null
的列,这是不允许的。最初,当我在字段中使用
date_of_birth = models.DateField(max_length = 100, null = True)
时。系统提示我添加一个默认值,并在bash终端中添加了timezone.now()
。我不确定这解释了错误的原因,但是我还是添加了它。 最佳答案
列“ date_of_birth”中的null
值违反了非空约束
您可能没有将任何值保存到DOB
列,需要允许NULL
值。您应该更改为
DOB = models.DateField(auto_now = False, null = True)
您还应该确保表中的
DOB
列允许使用null
。