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
Modified build_relational_field() to change its behaviour if relation…
…ship is reverse vs forward to ensure correct field checking
  • Loading branch information
bphillips committed Dec 3, 2015
commit 21839e45c6da35ea8c2155b7840d0d0cd863f0b0
11 changes: 8 additions & 3 deletions rest_framework/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1131,9 +1131,14 @@ def build_relational_field(self, field_name, relation_info):
field_kwargs = get_relation_kwargs(field_name, relation_info)

to_field = field_kwargs.pop('to_field', None)
if to_field and not relation_info.related_model._meta.get_field(to_field).primary_key:
field_kwargs['slug_field'] = to_field
field_class = self.serializer_related_to_field
if relation_info.reverse:
if to_field and not relation_info.related_model_field.related_field.primary_key:
field_kwargs['slug_field'] = to_field
field_class = self.serializer_related_to_field
else:
if to_field and not relation_info.related_model._meta.get_field(to_field).primary_key:
field_kwargs['slug_field'] = to_field
field_class = self.serializer_related_to_field

# `view_name` is only valid for hyperlinked relationships.
if not issubclass(field_class, HyperlinkedRelatedField):
Expand Down
2 changes: 1 addition & 1 deletion rest_framework/utils/field_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def get_relation_kwargs(field_name, relation_info):
"""
Creates a default instance of a flat relational field.
"""
model_field, related_model, to_many, to_field, has_through_model = relation_info
model_field, related_model, related_model_field, to_many, to_field, has_through_model, reverse = relation_info
kwargs = {
'queryset': related_model._default_manager,
'view_name': get_detail_view_name(related_model)
Expand Down
22 changes: 16 additions & 6 deletions rest_framework/utils/model_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
RelationInfo = namedtuple('RelationInfo', [
'model_field',
'related_model',
'related_model_field',
'to_many',
'to_field',
'has_through_model'
'has_through_model',
'reverse'
])


Expand Down Expand Up @@ -108,22 +110,26 @@ def _get_forward_relationships(opts):
forward_relations[field.name] = RelationInfo(
model_field=field,
related_model=_resolve_model(field.rel.to),
related_model_field=None,
to_many=False,
to_field=_get_to_field(field),
has_through_model=False
has_through_model=False,
reverse=False
)

# Deal with forward many-to-many relationships.
for field in [field for field in opts.many_to_many if field.serialize]:
forward_relations[field.name] = RelationInfo(
model_field=field,
related_model=_resolve_model(field.rel.to),
related_model_field=None,
to_many=True,
# manytomany do not have to_fields
to_field=None,
has_through_model=(
not field.rel.through._meta.auto_created
)
),
reverse=False
)

return forward_relations
Expand All @@ -144,9 +150,11 @@ def _get_reverse_relationships(opts):
reverse_relations[accessor_name] = RelationInfo(
model_field=None,
related_model=related,
related_model_field=relation.field,
to_many=relation.field.rel.multiple,
to_field=_get_to_field(relation.field.model._meta.pk),
has_through_model=False
to_field=_get_to_field(relation.field),
has_through_model=False,
reverse=True
)

# Deal with reverse many-to-many relationships.
Expand All @@ -156,13 +164,15 @@ def _get_reverse_relationships(opts):
reverse_relations[accessor_name] = RelationInfo(
model_field=None,
related_model=related,
related_model_field=relation.field,
to_many=True,
# manytomany do not have to_fields
to_field=None,
has_through_model=(
(getattr(relation.field.rel, 'through', None) is not None) and
not relation.field.rel.through._meta.auto_created
)
),
reverse=True
)

return reverse_relations
Expand Down