NOT NULL约束失败:IntegrityError

时间:2017-12-14 07:41:52

标签: django-models django-views

从文档中尝试django项目。 这是我的Models.py 我正在努力建立一个简单的员工目标管理系统。我还使用了DRF和序列化器,集成了swagger,只对下面的模型进行'GET'。我已删除(注释掉)所有ForeignKey和ManyToMany字段,当我添加修改目标或项目时,我收到此错误。我似乎无法解决这个问题,请帮忙。

 class Department(models.Model):
    department_name = models.CharField(max_length=200)
    department_location = models.CharField(max_length=200)
    department_region = models.CharField(max_length=200)
    department_site = models.CharField(max_length=200)
    department_job_titles = models.CharField(max_length=200)

    def __str__(self):
        return self.department_name


class Employee(models.Model):
    #department_name = models.ManyToManyField(Department)
    first_name = models.CharField(max_length=200)
    last_name = models.CharField(max_length=200)
    email_id = models.CharField(max_length=100)
    #employee_id = models.IntegerField()
    supervisor_name = models.CharField(max_length=100)
    designation = models.CharField(max_length=100)
    doj = models.DateTimeField('date joined')

    def __str__(self):
        return self.first_name

class Objectives(models.Model):
    objective_name = models.CharField(max_length=200)
    #employee = models.ManyToManyField(Employee)

    def __str__(self):
        return self.objective_name


class Project(models.Model):
    #employee = models.ManyToManyField(Employee)
    project_name = models.CharField(max_length=200)

    def __str__(self):
        return self.project_name

class Goals(models.Model):
    goal_id = models.IntegerField()
    goal_name = models.CharField(max_length=200)
    #department = models.ManyToManyField(Department)
    role = models.CharField(max_length=200)
    #objective_id = models.ManyToManyField(Objectives)
    goal_start = models.DateTimeField('date goal added')
    goal_end = models.DateTimeField('date goal ends')
    #employee = models.ManyToManyField(Employee)

    def __str__(self):
        return self.goal_name

这是我的views.py

from django.shortcuts import get_object_or_404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .models import Employee
from .models import Goals
from .models import Objectives
from .models import Department
from .models import Project
from .serializers import EmployeeSerializer
from .serializers import ProjectSerializer
from .serializers import GoalsSerializer
from .serializers import ObjectivesSerializer
from .serializers import DepartmentSerializer
from django.http import HttpResponse
from rest_framework_swagger.views import get_swagger_view


schema_view = get_swagger_view(title='UpYourGame API')

#just a view if someone hits the index
def index(request):
    return HttpResponse("Hello and Welcome to UpyourGame")


# this will be the URL /employee/
class Employee(APIView):

    def get(self, request):
        employee = Employee.objects.all()
        serializer = EmployeeSerializer(employee, many=True)
        return Response(serializer.data)

class Project(APIView):
    def get(self, request):
        project = Project.objects.all()
        serializer = ProjectSerializer(project, many=True)
        return Response(serializer.data)


class Goals(APIView):

    def get(self, request):
        goals = Goals.objects.all()
        serializer = GoalsSerializer(goals, many=True)
        return Response(serializer.data)

class Objectives(APIView):
    def get(self, request):
        objectives = Objectives.objects.all()
        serializer = ObjectivesSerializer(objectives, many=True)
        return Response(serializer.data)

#class Company(APIView):
 #   def get(self, request):
  #      company = Company.objects.all()
   #     serializer = CompanySerializer(company, many=False)
    #    return Response(serializer.data)

class Department(APIView):
    def get(self, request):
        department = Department.objects.all()
        serializer = DepartmentSerializer(Department, many=True)
        return Response(serializer.data)

这是myproject / urls.py

from django.conf.urls import include, url
from django.contrib import admin
from enterprise import views


urlpatterns = [

    url(r'^$', views.schema_view, name='schema_view'),
    url(r'^enterprise/', include('enterprise.urls')),
    url(r'^admin/', admin.site.urls),
    url(r'^employee/', views.Employee.as_view()),
    url(r'^goals/', views.Goals.as_view()),
    url(r'^department/', views.Department.as_view()),
    url(r'^department/', views.Department.as_view()),
    url(r'^objectives/', views.Objectives.as_view()),
    url(r'^project/', views.Project.as_view()),

]

这是我的serializers.py

from rest_framework import serializers
from .models import Employee
from .models import Goals
from .models import Objectives
from .models import Department
from .models import Project


class EmployeeSerializer(serializers.ModelSerializer):

    class Meta:
        model = Employee
        fields = '__all__'


#class EmployeeDetailsSerializer(serializers.ModelSerializer):

 #   class Meta:
  #      model = EmployeeDetails
   #     fields = '__all__'

class ObjectivesSerializer(serializers.ModelSerializer):

    class Meta:
        model = Objectives
        fields = '__all__'


class DepartmentSerializer(serializers.ModelSerializer):

    class Meta:
        model = Department
        fields = '__all__'

#class CompanySerializer(serializers.ModelSerializer):

#    class Meta:
#        model = Company
#        fields = '__all__'

class GoalsSerializer(serializers.ModelSerializer):

    class Meta:
        model = Goals
        fields = '__all__'

class ProjectSerializer(serializers.ModelSerializer):

    class Meta:
        model = Project
        fields = '__all__'

1 个答案:

答案 0 :(得分:0)

由于迁移过多,SQLlite数据库已损坏,可能会发生。我删除了SQLlite和我的所有迁移,我重新尝试了migrate,makemigrate和sqlmigrate,它解决了这个问题。 如果您在开发环境中遇到类似问题,请尝试以下操作 1.删​​除migrations文件夹中的所有迁移 2.删除db.sqlite3 3.运行python manage.py migrate 4.运行python manage.py makemigrations 5.运行python manage.py sqlmigrate appname 0001

这将解决此问题。