# Buggy code
def get_field_type_from_lookup(
    ctx: MethodContext,
    django_context: DjangoContext,
    model_cls: Type[Model],
    *,
    method: str,
    lookup: str,
) -> Optional[MypyType]:
    try:
        lookup_field = django_context.resolve_lookup_into_field(model_cls, lookup)
    except FieldError as exc:
        ctx.api.fail(exc.args[0], ctx.context)
        return None
    except LookupsAreUnsupported:
        return AnyType(TypeOfAny.explicit)

    if isinstance(lookup_field, RelatedField) and lookup_field.column == lookup:
        related_model_cls = django_context.get_field_related_model_cls(lookup_field)
        lookup_field = django_context.get_primary_key_field(related_model_cls)

    field_get_type = django_context.get_field_get_type(
        helpers.get_typechecker_api(ctx), lookup_field, method=method
    )
    return field_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 get_field_type_from_lookup(
    ctx: MethodContext,
    django_context: DjangoContext,
    model_cls: Type[Model],
    *,
    method: str,
    lookup: str,
) -> Optional[MypyType]:
    try:
        lookup_field = django_context.resolve_lookup_into_field(model_cls, lookup)
    except FieldError as exc:
        ctx.api.fail(exc.args[0], ctx.context)
        return None
    except LookupsAreUnsupported:
        return AnyType(TypeOfAny.explicit)

    if isinstance(lookup_field, RelatedField) and lookup_field.column == lookup:
        related_model_cls = django_context.get_field_related_model_cls(lookup_field)
        if related_model_cls is None:
            return AnyType(TypeOfAny.from_error)
        lookup_field = django_context.get_primary_key_field(related_model_cls)

    field_get_type = django_context.get_field_get_type(
        helpers.get_typechecker_api(ctx), lookup_field, method=method
    )
    return field_get_type