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
}