Skip to content

Parser: django-mptt models are skipped entirely (MPTTModel base + TreeForeignKey) #49

Description

@FROWNINGdev

Context

Models built on django-mptt are currently invisible to the parser — not merely missing a field, but skipped entirely.

_looks_like_model (cli/django_orm_lens/parser.py:358) decides whether a class is a Django model from its base classes. It accepts models.Model, a handful of common abstract names, anything matching Abstract[A-Z], and anything ending in Mixin. MPTTModel matches none of them, so:

class Category(MPTTModel):          # skipped — never appears in the sidebar,
    name = models.CharField(max_length=50)   # ER diagram, or any analyzer
    parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True)

The second half of the problem is TreeForeignKey / TreeOneToOneField / TreeManyToManyField. These are thin subclasses of the corresponding Django fields, but the parser matches field types against the literal whitelist in BARE_FIELD_TYPES (parser.py:59) and relations against RELATION_TYPES (parser.py:38), so even inside a detected model they would be dropped.

What to change

  1. Teach _looks_like_model about MPTTModel (and MPTTMeta-style bases if you find others worth adding).
  2. Add the three Tree* field names to BARE_FIELD_TYPES and to RELATION_TYPES, so _extract_related / _extract_on_delete / _extract_related_name (lines 221–278) treat them exactly like their Django counterparts. TreeForeignKey('self', ...) must produce the same self-referential edge that a plain ForeignKey('self', ...) does — there is already a test for the self-FK case at cli/tests/test_er_formats.py:112.

Please keep this additive: no django-mptt import, no new dependency. The parser is deliberately pure-static and must keep working on a project whose venv is broken.

Acceptance criteria

  • A model inheriting MPTTModel shows up in django-orm-lens scan with its fields.
  • TreeForeignKey('self', ...) renders a self-edge in er, honouring on_delete and related_name.
  • A fixture under cli/tests/fixtures/ covering both, wired into the test suite.

Getting started

pip install -e "cli[dev]"
pytest cli/tests -v -k parser

Note this is one half of the "third-party field support" roadmap item; django-taggit is tracked separately in #50 — feel free to take either or both.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions