Fixed a deprecation warning by thierryba · Pull Request #5058 · encode/django-rest-framework (original) (raw)

@thierryba OK. Thanks for the follow-up.

@encode/django-rest-framework-core-team Can someone check my reasoning here... — I don't think ModelField will ever be relational:

First we fetch the target field info:

def get_field_info(model):
"""
Given a model class, returns a `FieldInfo` instance, which is a
`namedtuple`, containing metadata about the various field types on the model
including information about their relationships.
"""
opts = model._meta.concrete_model._meta
pk = _get_pk(opts)
fields = _get_fields(opts)
forward_relations = _get_forward_relationships(opts)
reverse_relations = _get_reverse_relationships(opts)
fields_and_pk = _merge_fields_and_pk(pk, fields)
relationships = _merge_relationships(forward_relations, reverse_relations)
return FieldInfo(pk, fields, forward_relations, reverse_relations,
fields_and_pk, relationships)

Then build_field uses this, as the info parameter:

def build_field(self, field_name, info, model_class, nested_depth):
"""
Return a two tuple of (cls, kwargs) to build a serializer field with.
"""
if field_name in info.fields_and_pk:
model_field = info.fields_and_pk[field_name]
return self.build_standard_field(field_name, model_field)
elif field_name in info.relations:
relation_info = info.relations[field_name]
if not nested_depth:
return self.build_relational_field(field_name, relation_info)
else:

But question: won't a relational field always end up coming out of build_relational_field — which never returns ModelField?

If so then the correct fix here is to just delete the whole if rel is not None branch.

  1. What did I miss?
  2. How do I construct a simple test case for that?

Ta!