diff --git a/rest_framework/mixins.py b/rest_framework/mixins.py index 1edcfa5c9..8dc0c3295 100644 --- a/rest_framework/mixins.py +++ b/rest_framework/mixins.py @@ -113,6 +113,10 @@ class UpdateModelMixin(object): slug_field = self.get_slug_field() setattr(obj, slug_field, slug) + # Ensure we clean the attributes so that we don't eg return integer + # pk using a string representation, as provided by the url conf kwarg. + obj.full_clean() + class DestroyModelMixin(object): """ diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 02377492c..8156bc185 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -497,25 +497,27 @@ class ModelSerializer(Serializer): if instance is not None: for key, val in attrs.items(): setattr(instance, key, val) - return instance - # Reverse relations - for (obj, model) in self.opts.model._meta.get_all_related_m2m_objects_with_model(): - field_name = obj.field.related_query_name() - if field_name in attrs: - self.m2m_data[field_name] = attrs.pop(field_name) + else: + # Reverse relations + for (obj, model) in self.opts.model._meta.get_all_related_m2m_objects_with_model(): + field_name = obj.field.related_query_name() + if field_name in attrs: + self.m2m_data[field_name] = attrs.pop(field_name) - # Forward relations - for field in self.opts.model._meta.many_to_many: - if field.name in attrs: - self.m2m_data[field.name] = attrs.pop(field.name) + # Forward relations + for field in self.opts.model._meta.many_to_many: + if field.name in attrs: + self.m2m_data[field.name] = attrs.pop(field.name) + + instance = self.opts.model(**attrs) - instance = self.opts.model(**attrs) try: instance.full_clean(exclude=self.get_validation_exclusions()) except ValidationError, err: self._errors = err.message_dict return None + return instance def save(self, save_m2m=True): diff --git a/rest_framework/tests/generics.py b/rest_framework/tests/generics.py index 14fa66f93..7c24d84ea 100644 --- a/rest_framework/tests/generics.py +++ b/rest_framework/tests/generics.py @@ -175,7 +175,7 @@ class TestInstanceView(TestCase): content = {'text': 'foobar'} request = factory.put('/1', json.dumps(content), content_type='application/json') - response = self.view(request, pk=1).render() + response = self.view(request, pk='1').render() self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals(response.data, {'id': 1, 'text': 'foobar'}) updated = self.objects.get(id=1)