2017-10-06 98 views
0

我正在构建一个电子健康记录软件。我的一个观点包含一个捕获患者基本信息的表单。修复“date_of_birth”列中的空值违反了非空约束

forms.py

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, [email protected], null, 1991-12-10). 

我对数据库使用postgresql 9.6。我删除了迁移文件夹并多次迁移模型,但仍然存在nullIntegrityError。我也一直在我的数据库中查找数据库表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()。我不确定这解释了错误背后的原因,但我仍然加入了它。在列

回答

0

null值“DATE_OF_BIRTH”违反

您可能没有任何值保存到DOB列非空约束,你需要允许NULL值。你应该改变以

DOB = models.DateField(auto_now = False, null = True) 

你也应该确保DOB列在表允许null

相关问题