diff --git a/rest_framework/relations.py b/rest_framework/relations.py index eac9647b00..1ca1a03ec8 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -492,10 +492,19 @@ def to_internal_value(self, data): if not self.allow_empty and len(data) == 0: self.fail('empty') - return [ - self.child_relation.to_internal_value(item) - for item in data - ] + if isinstance(self.child_relation, PrimaryKeyRelatedField): + values = list(self.child_relation.get_queryset().filter(pk__in=data)) + missing_primary_keys = set(v.pk for v in values) - set(data) + + if missing_primary_keys: + self.fail('missing_ids', ids_not_found=list(missing_primary_keys)) + else: + values = [ + self.child_relation.to_internal_value(item) + for item in data + ] + + return values def get_attribute(self, instance): # Can't have any relationships if not created