fix(python): address the review of the Django PRs #6542-#6545 (#6656)

Follow-up fixes to the Django support:
- manage.py tasks: manage.py is looked up on each use and only a successful command list is cached; lines run while help loads go to manage.py; a failed help is reported; the active file's project picks the manage.py in a monorepo (shared via platform/django.ts); py/python3.x/Windows forms count as manage.py lines.
- Django console: keeps the project root as working directory with manage.py's folder on sys.path, reads setdefault and os.environ[...] settings forms, and shows auto-imported names once reassigned.
- URL names: re-reading names clears navigation's cache, names nest under include namespaces, and only path/re_path/url name= counts (small Python scanner).
- Field lookups: models keyed per app, multi-table inheritance reverse names fixed, related_query_name/related_name/%(class)s rules, relation lookups and <fk>_id, completion in Q(...) and after F(...).

Co-authored-by: wildcatDownstairs <[email protected]>
Цей коміт міститься в:
LU XIANGRUIтаwildcatDownstairs authored and GitHub committed 2026-11-28 20:31:33 +08:00
1 джерело 68abd00a7f
коміт 9d92f47f94
19 files changed
+1158 -262

No files matched your search

+59 -20
Переглянути файл
@@ -71,23 +71,62 @@ it.skipIf(!IPYTHON)("ends at exit() under IPython", () => {
expect([text, messages.length, status]).toEqual(["", 2, 0]);
});
it.skipIf(!DJANGO)("sets Django up and imports the models like manage.py shell", () => {
const cwd = mkdtempSync(join(tmpdir(), "console-django-"));
writeFileSync(
join(cwd, "site_settings.py"),
'SECRET_KEY = "x"\nINSTALLED_APPS = ["django.contrib.contenttypes", "django.contrib.auth"]\n' +
'DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}}\n',
);
// As the IDE runs it: `-c`, so the working directory (manage.py's) is on sys.path.
const result = spawnSync(DJANGO!, ["-u", "-c", readFileSync(DRIVER, "utf8")], {
cwd,
input: request("User._meta.label, settings.SECRET_KEY"),
encoding: "utf8",
env: { ...process.env, OPENIDEA_DJANGO_SETTINGS: "site_settings", DJANGO_SETTINGS_MODULE: "" },
});
const { text, messages } = stdoutParser()(result.stdout);
expect(messages[0]).toMatchObject({ ready: { django: expect.stringMatching(/^\d/) } });
expect(text).toContain("('auth.User', 'x')");
// The imports are the console's own, not the user's variables.
expect(variables(messages)[0]).toEqual([]);
});
it.skipIf(!DJANGO)(
"sets Django up and imports the models like manage.py shell",
() => {
const site = mkdtempSync(join(tmpdir(), "console-django-"));
writeFileSync(
join(site, "site_settings.py"),
'SECRET_KEY = "x"\nINSTALLED_APPS = ["django.contrib.contenttypes", "django.contrib.auth"]\n' +
'DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}}\n',
);
// As the IDE runs it: `-c`, in the project root, with manage.py's folder given apart.
const result = spawnSync(DJANGO!, ["-u", "-c", readFileSync(DRIVER, "utf8")], {
cwd: tmpdir(),
input: [
"User._meta.label",
"import django\ndjango.VERSION >= (6,) and settings.SECRET_KEY",
"x = 1",
"User = 'mine'",
]
.map(request)
.join(""),
encoding: "utf8",
env: {
...process.env,
OPENIDEA_DJANGO_DIR: site,
OPENIDEA_DJANGO_SETTINGS: "site_settings",
DJANGO_SETTINGS_MODULE: "",
},
});
const { text, messages } = stdoutParser()(result.stdout);
expect(result.stderr).toBe("");
expect(messages[0]).toMatchObject({ ready: { django: expect.stringMatching(/^\d/) } });
// 5.2+ imports the models; 6.0+ also `settings` (False on 5.2).
expect(text).toMatch(/^'auth\.User'\n('x'|False)\n/);
// The imports are the console's own, not the user's variables, until the user assigns one.
expect(variables(messages)[2]).toEqual(["x: int = 1"]);
expect(variables(messages)[3]).toEqual(["User: str = 'mine'", "x: int = 1"]);
},
30_000,
);
it.skipIf(!DJANGO)(
"uses DJANGO_SETTINGS_MODULE from the environment when manage.py names none",
() => {
const site = mkdtempSync(join(tmpdir(), "console-django-"));
writeFileSync(join(site, "env_settings.py"), 'SECRET_KEY = "x"\nINSTALLED_APPS = []\n');
const ready = (env: Record<string, string>) => {
const result = spawnSync(DJANGO!, ["-u", "-c", readFileSync(DRIVER, "utf8")], {
cwd: tmpdir(),
input: "",
encoding: "utf8",
env: { ...process.env, OPENIDEA_DJANGO_DIR: site, ...env },
});
return stdoutParser()(result.stdout).messages[0];
};
expect(ready({ DJANGO_SETTINGS_MODULE: "env_settings" })).toMatchObject({ ready: { django: expect.any(String) } });
expect(ready({ DJANGO_SETTINGS_MODULE: "" })).not.toHaveProperty("ready.django");
},
30_000,
);