mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-05 13:00:12 +03:00
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:
parent
8263048af4
commit
d47f29a207
|
@ -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):
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user