[GSoC 2021] Adapt Schema Editors to operate from model states instead of fake rendered models

While writing the code for the central registry in ProjectState. I came across a problem which is mentioned below. I am trying to figure out the solution for the same. I would be glad if anyone may help me out with this.

Problem

To initialize the central registry in django.db.migrations.state.ProjectState.add_model I need the following parameters

  • from_app_label
  • from model_name
  • from field
  • to model_name
  • to app_label
  • to_field

Out of all these required values I figured out how to get the first 5 values as follows:

  • from_app_label - model_State.app_label
  • from model_name - model_state.name_lower
  • from field - model_state.fields.items()
  • to model_name - model_state.fields.items().field.remote_field.model
  • to app_label - model_state.fields.items().field.remote_field.model.split(".")

But I am unable to figure out how can I get to_field in case the to_field is not explicitly defined by the developer.

Suggestions and feedback would be highly appreciated.

Regard,
Manav