我有以下型号
class Peri(models.Model):
date = models.DateField()
customer = models.ForeignKey(Customer)
class PeriTask(models.Model):
#fields
peri = models.ForeignKey(Peri)
我的序列化器如下
class PeriSerializer(serializers.HyperlinkedModelSerializer):
customer = serializers.PrimaryKeyRelatedField(read_only=True)
class Meta:
model = Peri
fields = ('id', 'date', 'url', 'peritasks', 'customer')
class PeriTaskSerialiazer(serializers.HyperlinkedModelSerializer):
tooth = serializers.PrimaryKeyRelatedField(read_only=True)
class Meta:
model = PeriTask
fields = ('id', 'task_type', 'implant', 'furcation', 'bleeding1', 'bleeding2', 'bleeding3', 'plaque1', 'plaque2',
'gingival_margin1', 'gingival_margin2', 'gingival_margin3', 'probing_depth1', 'probing_depth2',
'probing_depth3', 'tooth', 'url', )
我的视图集
class PeriodontogrammaViewSet(ModelViewSet):
serializer_class = PeriSerializer
queryset = Peri.objects.all()
class PeriTaskViewSet(ModelViewSet):
serializer_class = PeriTaskSerialiazer
queryset = PeriTask.objects.all()
但是当我尝试使用api创建一个新的peri时,它会给我以下完整性错误
NOT NULL constraint failed: peri_peri.customer_id
我发布的json数据是
{"date": "2014-12-17",
"customer": 27
}
我没有为客户创建序列化器,因为我对其他型号的api不感兴趣。
答案 0 :(得分:1)
在序列化程序中,您已将客户密钥设置为read_only
:
customer = serializers.PrimaryKeyRelatedField(read_only=True)
尝试将其设置为False
或只是删除整行(这对我来说似乎是多余的)