4
votes

My models:

class ContentHotel(models.Model):
    hotel_id = models.IntegerField(unique=True, blank=True, primary_key=True)

    class Meta:
        managed = False
        db_table = 'content_hotels'
        ordering = ('hotel_id',)

    def __str__(self):
        return str(self.hotel_id)


class RateHotel(models.Model):
    rate_hotel_id = models.IntegerField(blank=True, primary_key=True, unique=True)
    content_hotel = models.ForeignKey(ContentHotel, on_delete=models.CASCADE, related_name='rate_hotel')

    class Meta:
        managed = False
        db_table = 'rate_hotels'
        ordering = ('rate_hotel_id',)

    def __str__(self):
        return str(self.rate_hotel_id)

My Serializers:

class RateHotelSerializer(serializers.ModelSerializer):

    class Meta:
        model = RateHotel
        fields = __all__


class ContentHotelSerializer(serializers.ModelSerializer):
    rate_hotel = RateHotelSerializer(many=True)

    class Meta:
        model = ContentHotel
        fields = ('hotel_id', 'rate_hotel')

    def create(self, validated_data):
        rate_hotels = validated_data.pop('rate_hotel')
        content_hotel = ContentHotel.objects.create(**validated_data)

        for rate_hotel in rate_hotels:
            RateHotel.objects.create(content_hotel=content_hotel, **rate_hotel)

        return content_hotel

JSON:

{
    "hotel_id": -1,
    "rate_hotel": [{"content_hotel": -1, "rate_hotel_id": 1}]
}

Above JSON input gives me error like:

{
    "rate_hotel": [
        {
            "content_hotel": [
                "Invalid pk \"1\" - object does not exist."
            ]
        }
    ],
    "status_code": 400
}

REFERENCE: http://www.django-rest-framework.org/api-guide/relations/#writable-nested-serializers

I referenced the link above, anyone knows how to address this? But f I create the two objects separately, it works correctly, like this:

{
    "hotel_id": -1,
}

{
    "content_hotel": -1, 
    "rate_hotel_id": 1
}
1
Can you check what is the value of rate_hotels in the .create() in both cases.ofnowhere
I think your error is with passing "rate_hotel_id": 1 in the first case, you should not pass the object of the id to be created.ofnowhere
It has nothing to do with the "rate_hotel_id", it is a validation error, just like what @changak mentioned.Ziru Liu

1 Answers

7
votes

The validation has been done before the serializer create function and because you haven't create the contenthotel with that pk yet, the pk is invalid for that field(content_hotel). make content_hotel readonly in RateHotelSerializer and the problem will be fixed, change the serializer to this:

class RateHotelSerializer(serializers.ModelSerializer):

    class Meta:
        model = RateHotel
        fields = __all__
        read_only_fields = ('content_hotel', )

and also now you don't need to add content_hotel in objects of the list for rate_hotel, use a json like this:

{
    "hotel_id": -1,
    "rate_hotel": [{"rate_hotel_id": 1}]
}