Fixed the rest of the test cases.

# Some tests do not have the full QuerySet API
# But we optimize SlugRelatedFields by only loading what we need

# it seems that some tests/django initializers are setting
# to_field where it is totally unnecessary so SlugRelatedField was used… Added checks
This commit is contained in:
Tom Jaster 2015-01-19 16:00:21 +01:00
parent 8263048af4
commit d47f29a207
2 changed files with 12 additions and 2 deletions

View File

@ -303,7 +303,14 @@ class SlugRelatedField(RelatedField):
def to_internal_value(self, data):
try:
return self.get_queryset().only(self.slug_field).get(**{self.slug_field: data})
# Some tests do not have the full QuerySet API
# But we optimize SlugRelatedFields by only loading what we need
qs = self.get_queryset()
if hasattr(qs, 'only'):
return self.get_queryset().only(self.slug_field).get(**{self.slug_field: data})
else:
return self.get_queryset().get(**{self.slug_field: data})
except ObjectDoesNotExist:
self.fail('does_not_exist', slug_name=self.slug_field, value=smart_text(data))
except (TypeError, ValueError):

View File

@ -1005,7 +1005,10 @@ class ModelSerializer(Serializer):
else:
kwargs = get_relation_kwargs(field_name, relation_info)
to_field = kwargs.get('to_field', False)
if to_field:
kwargs.pop('to_field', None)
# it seems that some tests/django initializers are setting
# to_field where it is totally unnecessary
if to_field and to_field != 'id':
# using the slug field for now
kwargs.pop('to_field', None)
kwargs['slug_field'] = to_field