简体   繁体   English

Django rest框架外键约束失败创建模型

[英]Django rest framework foreign key constraint fails creating model

I have the following in a Django Rest Framework setup: 我在Django Rest Framework设置中有以下内容:

models.py: models.py:

class Sku(BaseModel):
    sku_code = models.CharField(max_length=18, primary_key=True)
    supplier_id = models.PositiveIntegerField(db_index=True)
    soh = models.PositiveIntegerField(default=0)
    reserved = models.PositiveIntegerField(default=0)
    broken = models.PositiveIntegerField(default=0)
    unallocated = models.PositiveIntegerField(default=0)
    reorder = models.PositiveIntegerField(default=0)

class Reservation(BaseModel):
    sku = models.ForeignKey(Sku, db_column='sku_code')
    order_id = models.PositiveIntegerField(db_index=True)

serializers.py: serializers.py:

class SkuSerializer(serializers.ModelSerializer):
    class Meta:
        model = Sku
        fields = (
            'sku_code',
            'supplier_id',
            'soh',
            'reserved',
            'broken',
            'unallocated',
            'reorder',
            'created_at',
            'modified_at',
        )


class ReservationSerializer(serializers.ModelSerializer):
    sku = SkuSerializer(read_only=True)

    class Meta:
        model = Reservation
        fields = (
            'id',
            'order_id',
            'sku',
            'created_at',
            'modified_at'
        )

views.py: views.py:

class ReservationList(mixins.CreateModelMixin,
                      generics.GenericAPIView):

    queryset = Reservation.objects.all()
    serializer_class = ReservationSerializer

    def post(self, request, *args, **kwargs):
        sku = get_object_or_404(Sku, sku_code=request.data['sku_code'])
        request.data['sku'] = sku
        return self.create(request, *args, **kwargs)

Now when I post to the url linked to ReservationList.post view above I get the error: IntegrityError: (1048, "Column 'sku_code' cannot be null") . 现在当我发布链接到上面的ReservationList.post视图的url时,我得到错误: IntegrityError: (1048, "Column 'sku_code' cannot be null")

It seems to be bypassing the serializers validation and failing on the database layer. 它似乎绕过了序列化程序验证并在数据库层上失败了。 For some reason it doesn't accept the SKU being passed in. 由于某种原因,它不接受传入的SKU。

What am I doing wrong here? 我在这做错了什么? I have tried to follow the example at http://www.django-rest-framework.org/api-guide/relations/#nested-relationships but this seems to break down with the CreateModelMixin . 我试图按照http://www.django-rest-framework.org/api-guide/relations/#nested-relationships中的示例进行操作,但这似乎与CreateModelMixin分解。 I can't tell if there is something wrong with how my models or serializers are set up. 我无法判断我的模型或序列化器的设置方式是否有问题。

You've set the sku field to read only , that's why the serializer is ignoring it when you post. 您已将sku字段设置为read only ,这就是当您发布时序列化程序忽略它的原因。

From the relevant documentation 相关文档

Read-only fields are included in the API output, but should not be included in the input during create or update operations. 只读字段包含在API输出中,但在创建或更新操作期间不应包含在输入中。 Any 'read_only' fields that are incorrectly included in the serializer input will be ignored. 任何错误包含在序列化程序输入中的'read_only'字段都将被忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM