# Buggy code
def run_with_model_cls(self, model_cls: Type[Model]) -> None:
    for field in model_cls._meta.get_fields():
        if isinstance(field, ForeignKey):
            related_model_cls = self.django_context.get_field_related_model_cls(field)
            rel_primary_key_field = self.django_context.get_primary_key_field(
                related_model_cls
            )
            field_info = self.lookup_class_typeinfo_or_incomplete_defn_error(
                rel_primary_key_field.__class__
            )
            is_nullable = self.django_context.get_field_nullability(field, None)
            set_type, get_type = get_field_descriptor_types(field_info, is_nullable)
            self.add_new_node_to_model_class(
                field.attname, Instance(field_info, [set_type, get_type])
            )


# Traceback (AttributeError)
Traceback (most recent call last):
File "/usr/local/bin/mypy", line 10, in <module>
sys.exit(console_entry())
File "/usr/local/lib/python3.6/site-packages/mypy/__main__.py", line 8, in console_entry
main(None, sys.stdout, sys.stderr)
File "/usr/local/lib/python3.6/site-packages/mypy/main.py", line 83, in main
res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 164, in build
result = _build(sources, options, alt_lib_path, flush_errors, fscache, stdout, stderr)
File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 224, in _build
graph = dispatch(sources, manager, stdout)
File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 2526, in dispatch
graph = load_graph(sources, manager)
File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 2748, in load_graph
caller_state=st, caller_line=st.dep_line_map.get(dep, 1))
File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 1772, in __init__
self.compute_dependencies()
File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 2059, in compute_dependencies
self.manager.plugin.get_additional_deps(self.tree))
File "/usr/local/lib/python3.6/site-packages/mypy/plugin.py", line 743, in get_additional_deps
deps.extend(plugin.get_additional_deps(file))
File "/usr/local/lib/python3.6/site-packages/mypy_django_plugin/main.py", line 152, in get_additional_deps
related_model_module = related_model_cls.__module__
AttributeError: 'NoneType' object has no attribute '__module__'

# Fixed code
def run_with_model_cls(self, model_cls: Type[Model]) -> None:
    for field in model_cls._meta.get_fields():
        if isinstance(field, ForeignKey):
            related_model_cls = self.django_context.get_field_related_model_cls(field)
            if related_model_cls is None:
                error_context: Context = self.ctx.cls
                field_sym = self.ctx.cls.info.get(field.name)
                if field_sym is not None and field_sym.node is not None:
                    error_context = field_sym.node
                self.api.fail(
                    f"Cannot find model {field.related_model!r} "
                    f"referenced in field {field.name!r} ",
                    ctx=error_context,
                )
                self.add_new_node_to_model_class(
                    field.attname, AnyType(TypeOfAny.explicit)
                )
                continue

            rel_primary_key_field = self.django_context.get_primary_key_field(
                related_model_cls
            )
            field_info = self.lookup_class_typeinfo_or_incomplete_defn_error(
                rel_primary_key_field.__class__
            )
            is_nullable = self.django_context.get_field_nullability(field, None)
            set_type, get_type = get_field_descriptor_types(field_info, is_nullable)
            self.add_new_node_to_model_class(
                field.attname, Instance(field_info, [set_type, get_type])
            )