Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
* Removed SlugRelatedField optimization
* Moved compatibility check of to_fields to compat.py
* Expanded the check with a way that should work on django <1.6. Need to check trevis after pushing..
  • Loading branch information
Harper04 committed Jan 20, 2015
commit f808b6c6bc8c78df5bed5ddeb1556c21fc09bd5b
15 changes: 15 additions & 0 deletions rest_framework/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@ def __init__(self, *args, **kwargs):
super(MaxLengthValidator, self).__init__(*args, **kwargs)


# ForeignKey's to_fields parameter was changed in Django 1.6 to an array named to_fields
# to_fields is an array but django lets you only set one to_field for ForeignKeys
# https://docs.djangoproject.com/en/1.7/ref/models/fields/#django.db.models.ForeignKey.to_field
# see also ForeignKey and ForeignObject in
# https://docs.djangoproject.com/en/1.7/_modules/django/db/models/fields/related/

if django.VERSION >= (1, 6):
def get_to_field(field):
# This should work for casual ForeignKeys
return field.to_fields[0] if len(field.to_fields) else None
else:
def get_to_field(field):
return field.to_field
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still have to work on that one. Reading some more of that old code.



# URLValidator only accepts `message` in 1.6+
if django.VERSION >= (1, 6):
from django.core.validators import URLValidator
Expand Down
9 changes: 1 addition & 8 deletions rest_framework/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,14 +303,7 @@ def __init__(self, slug_field=None, **kwargs):

def to_internal_value(self, data):
try:
# 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})

return self.get_queryset().only(self.slug_field).get(**{self.slug_field: data})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure the only optimization is unrelated and should be removed.

except ObjectDoesNotExist:
self.fail('does_not_exist', slug_name=self.slug_field, value=smart_text(data))
except (TypeError, ValueError):
Expand Down
18 changes: 3 additions & 15 deletions rest_framework/utils/model_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.core.exceptions import ImproperlyConfigured
from django.db import models
from django.utils import six
from rest_framework.compat import get_to_field
from rest_framework.compat import OrderedDict
import inspect

Expand Down Expand Up @@ -97,18 +98,11 @@ def _get_forward_relationships(opts):
"""
forward_relations = OrderedDict()
for field in [field for field in opts.fields if field.serialize and field.rel]:
# For < django 1.6
if hasattr(field, 'to_fields'):
to_field = field.to_fields[0] if len(field.to_fields) else None
else:
to_field = None

forward_relations[field.name] = RelationInfo(
model_field=field,
related=_resolve_model(field.rel.to),
to_many=False,
# to_fields is an array but django lets you only set one to_field
to_field=to_field,
to_field=get_to_field(field),
has_through_model=False
)

Expand All @@ -135,17 +129,11 @@ def _get_reverse_relationships(opts):
reverse_relations = OrderedDict()
for relation in opts.get_all_related_objects():
accessor_name = relation.get_accessor_name()
# For < django 1.6
if hasattr(relation.field, 'to_fields'):
to_field = relation.field.to_fields[0] if len(relation.field.to_fields) else None
else:
to_field = None

reverse_relations[accessor_name] = RelationInfo(
model_field=None,
related=relation.model,
to_many=relation.field.rel.multiple,
to_field=to_field,
to_field=get_to_field(relation.field),
has_through_model=False
)

Expand Down