Minimal fix that addresses test case

This commit is contained in:
Tom Christie 2016-10-13 12:00:37 +01:00
parent c9bdd6a5b8
commit a490e99b03
3 changed files with 12 additions and 7 deletions

View File

@ -1167,7 +1167,7 @@ class ModelSerializer(Serializer):
field_kwargs = get_relation_kwargs(field_name, relation_info) field_kwargs = get_relation_kwargs(field_name, relation_info)
to_field = field_kwargs.pop('to_field', None) to_field = field_kwargs.pop('to_field', None)
if to_field and not relation_info.related_model._meta.get_field(to_field).primary_key: if to_field and not relation_info.reverse and not relation_info.related_model._meta.get_field(to_field).primary_key:
field_kwargs['slug_field'] = to_field field_kwargs['slug_field'] = to_field
field_class = self.serializer_related_to_field field_class = self.serializer_related_to_field

View File

@ -238,7 +238,7 @@ def get_relation_kwargs(field_name, relation_info):
""" """
Creates a default instance of a flat relational field. 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, to_many, to_field, has_through_model, reverse = relation_info
kwargs = { kwargs = {
'queryset': related_model._default_manager, 'queryset': related_model._default_manager,
'view_name': get_detail_view_name(related_model) 'view_name': get_detail_view_name(related_model)

View File

@ -23,7 +23,8 @@ RelationInfo = namedtuple('RelationInfo', [
'related_model', 'related_model',
'to_many', 'to_many',
'to_field', 'to_field',
'has_through_model' 'has_through_model',
'reverse'
]) ])
@ -81,7 +82,8 @@ def _get_forward_relationships(opts):
related_model=get_related_model(field), related_model=get_related_model(field),
to_many=False, to_many=False,
to_field=_get_to_field(field), to_field=_get_to_field(field),
has_through_model=False has_through_model=False,
reverse=False
) )
# Deal with forward many-to-many relationships. # Deal with forward many-to-many relationships.
@ -94,7 +96,8 @@ def _get_forward_relationships(opts):
to_field=None, to_field=None,
has_through_model=( has_through_model=(
not get_remote_field(field).through._meta.auto_created not get_remote_field(field).through._meta.auto_created
) ),
reverse=False
) )
return forward_relations return forward_relations
@ -118,7 +121,8 @@ def _get_reverse_relationships(opts):
related_model=related, related_model=related,
to_many=get_remote_field(relation.field).multiple, to_many=get_remote_field(relation.field).multiple,
to_field=_get_to_field(relation.field), to_field=_get_to_field(relation.field),
has_through_model=False has_through_model=False,
reverse=True
) )
# Deal with reverse many-to-many relationships. # Deal with reverse many-to-many relationships.
@ -135,7 +139,8 @@ def _get_reverse_relationships(opts):
has_through_model=( has_through_model=(
(getattr(get_remote_field(relation.field), 'through', None) is not None) and (getattr(get_remote_field(relation.field), 'through', None) is not None) and
not get_remote_field(relation.field).through._meta.auto_created not get_remote_field(relation.field).through._meta.auto_created
) ),
reverse=True
) )
return reverse_relations return reverse_relations