我已经定义了以下模型
from django.db import models
# Create your models here.
class Customer(models.Model):
cust_firstname=models.TextField(max_length=50)
cust_lastname=models.TextField(max_length=50)
cust_company=models.TextField(max_length=100)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
cust_contact_number = models.IntegerField()
cust_email = models.TextField(max_length=100)
cust_address=models.TextField(max_length=200,default=None)
def __unicode__(self):
return self.cust_firstname
class Employee(models.Model):
employee_firstname = models.TextField(max_length=50)
employee_lastname = models.TextField(max_length=50)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
employee_contact_number = models.IntegerField()
employee_email = models.TextField(max_length=100)
employee_designation = models.TextField(max_length=50)
employee_address=models.TextField(max_length=200, default=None)
def __unicode__(self):
return self.employee_firstname
class ProjectDetails(models.Model):
customer = models.ForeignKey(Customer)
employee=models.ForeignKey(Employee)
project_name = models.TextField(max_length=50)
project_startdate = models.DateField(auto_now=False, default=None)
project_status = models.TextField(default="Initial")
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.project_name
我的序列化器如下所示
from ProjectTemplate.models import Customer, Employee, ProjectDetails
from rest_framework import serializers
class CustomerSerializers(serializers.ModelSerializer):
class Meta:
model=Customer
fields = ('id','cust_firstname','cust_lastname','cust_company','created_at','updated_at','cust_contact_number','cust_email','cust_address')
read_only_fields = ('created_at', 'updated_at')
class EmployeeSerializer(serializers.ModelSerializer):
class Meta:
model=Employee
fields = ('id','employee_firstname','employee_lastname','created_at','updated_at','employee_contact_number','employee_email','employee_designation','employee_address')
read_only_fields = ('created_at', 'updated_at')
class ProjectDetailsSerializer(serializers.ModelSerializer):
#customer = CustomerSerializers(read_only=True)
#employee = EmployeeSerializer(read_only=True)
customer = serializers.SlugRelatedField(slug_field='cust_firstname', read_only=True)
employee = serializers.SlugRelatedField(slug_field='employee_firstname', read_only=True)
class Meta:
model = ProjectDetails
fields = ('id','project_name','project_startdate','created_at','updated_at','project_status','customer','employee')
read_only_fields = ('created_at', 'updated_at')
我在视图中的POST方法是
from django.http import Http404
from ProjectTemplate.models import ProjectDetails, Customer, Employee
from ProjectTemplate.serializers import ProjectDetailsSerializer, CustomerSerializers, EmployeeSerializer
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
class ProjectViewSet(viewsets.ModelViewSet):
queryset = ProjectDetails.objects.all()
serializer_class = ProjectDetailsSerializer
def perform_create(self, serializer):
custid = self.request.data.get("customer_id")
empid = self.request.data.get("employee_id")
serializer.save(
customer=Customer.objects.get(pk=custid),
employee=Employee.objects.get(pk=empid))
def perform_update(self, serializer):
custid = self.request.data.get("customer_id")
empid = self.request.data.get("employee_id")
serializer.save(
customer=Customer.objects.get(pk=custid),
employee=Employee.objects.get(pk=empid))
def perform_destroy(self, instance):
instance = self.get_object()
instance.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
当我尝试发布请求时,我收到完整性错误。 代码不保存对象,因为它没有获取customer_id外键值。请找到附加的屏幕截图。有些人可以帮我理解序列化方法中的错误。
答案 0 :(得分:0)
您收到此错误的原因是,当您保存serializer
customer
时,employee
未设置instance
。在serializer
本身上设置这些值根本不会产生任何影响。您必须在save
的{{1}}方法中传递这些附加值。
此外,我建议您覆盖serializer
方法,而不是perform_create
方法:
post
直接来自documentation:
使用mixin类提供了以下方法,并提供轻松覆盖对象保存或删除行为。
def perform_create(self, serializer): custid = self.request.data.get("customer_id") empid = self.request.data.get("employee_id") serializer.save( customer=Customer.objects.get(pk=custid), employee=Employee.objects.get(pk=empid) )
- 保存新对象实例时由perform_create(self, serializer)
调用。
ModelViewSet
时更新:
您不需要使用CreateModelMixin
,但可以使用APIView
进行不同的操作,例如ModelViewSet
,list
,retrieve
等。
create