Fields inherited from Django's `AbstractBaseUser` / `PermissionsMixin` / `AbstractUser` are missing
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 62/100
调研方向
Start in cli/orm_lens/parser.py: _abstract_bases_fields() near line 833 skips AbstractBaseUser because by_name.get(tail, []) finds nothing, and _attach_inherited_fields() near line 874 fills inherited_fields from that. Add a table of Django's own abstract bases (AbstractBaseUser, PermissionsMixin, AbstractUser) used only when no workspace class has that name. Done when the repro's drift reports nothing, describe lists the inherited fields, and the new tests plus the golden snapshot update pass with pytest, ruff and mypy.
由索引模型根据 Issue 内容生成。
描述
Source: taiga-back — taiga/users/models.py:117; pretix — base.User; label-studio — users.User; weblate — weblate/auth/models.py:942
What happens
A custom user model usually inherits its auth fields from Django:
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True)
password and last_login come from AbstractBaseUser, is_superuser, groups and user_permissions from PermissionsMixin. orm-lens only merges fields from abstract bases it can read in the workspace, and those two live in Django itself, so the fields are dropped. The same goes for AbstractUser (username, first_name, email, is_staff, date_joined, ...).
On all four projects above drift reports "migrated but no longer declared: groups, is_superuser, last_login, password, user_permissions", and the M2M edges to auth.Group / auth.Permission are missing from er and cascade.
Repro
accounts/models.py
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from django.db import models
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True)
USERNAME_FIELD = "email"
accounts/migrations/0001_initial.py (what makemigrations writes for it)
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [("auth", "0012_alter_user_first_name_max_length")]
operations = [
migrations.CreateModel(
name="User",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False)),
("password", models.CharField(max_length=128, verbose_name="password")),
("last_login", models.DateTimeField(blank=True, null=True, verbose_name="last login")),
("is_superuser", models.BooleanField(default=False)),
("email", models.EmailField(max_length=254, unique=True)),
("groups", models.ManyToManyField(blank=True, related_name="user_set", related_query_name="user", to="auth.group")),
("user_permissions", models.ManyToManyField(blank=True, related_name="user_set", related_query_name="user", to="auth.permission")),
],
),
]
$ orm-lens drift
~ accounts.user:
migrated but no longer declared: groups, is_superuser, last_login, password, user_permissions
summary: 1 model(s) drifted, 0 blocking, 1 migration(s) replayed
$ orm-lens describe accounts.User
| Field | Type | Args |
| --- | --- | --- |
| `email` | EmailField | `unique=True` |
With class User(AbstractUser) plus one own field, describe likewise shows only that field.
Expected: no drift; describe lists the inherited fields too, and groups / user_permissions are M2M relations to auth.Group / auth.Permission.
Where
cli/orm_lens/parser.py
- line 833,
_abstract_bases_fields():by_name.get(tail, [])finds nothing forAbstractBaseUser, so the base is skipped; - line 874,
_attach_inherited_fields(): fillsinherited_fieldsfrom that.
Suggested fix
Add a small table of Django's own abstract bases and their fields (as ParsedField, with inherited_from set):
AbstractBaseUser:password(CharField,max_length=128),last_login(DateTimeField,blank=True, null=True);PermissionsMixin:is_superuser(BooleanField),groups(ManyToManyField →auth.Group),user_permissions(ManyToManyField →auth.Permission);AbstractUser: both of the above plususername,first_name,last_name,email,is_staff,is_active,date_joined.
In _abstract_bases_fields(), when by_name has no class for the base name and the name is in the table, use the table's fields. A class with the same name in the workspace still wins, so a project that defines its own PermissionsMixin isn't affected. Note that is_active on AbstractBaseUser is a plain class attribute, not a field.
How to test
- A new test with the repro:
driftreports nothing,describelistspassword,last_login,is_superuser,groups,user_permissions,email. class User(AbstractUser)gets theAbstractUserfields; an ownemailshadows the inherited one.- A workspace class named
PermissionsMixin(abstract, with its own fields) is used instead of the table. - Run the golden snapshots: zulip's
UserProfileusesAbstractBaseUserandPermissionsMixin, so it will gain fields. Update the snapshot and list the changes in the PR. cd cli && python -m pytest -q,ruffandmypypass.
Difficulty: good first issue, a lookup table and one branch.
- 主要语言
- Python
- 星标
- 84
- 派生
- 13
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 提供 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
FROWNINGdev/django-orm-lens 的其他 Issue
-
suggest-indexes proposes composite indexes that include the primary key or a unique field可能已有人在做 @bfs2021 今天认领。 未关闭bug good first issue python
难度 2/5 1-3 小时 新手友好度 82/100
FROWNINGdev/django-orm-lens#269 ·
-
`migration-risk` text output prints `app.None` for graph-level findings可能已有人在做 @phoenixbycaselka 今天认领。 未关闭bug good first issue python
难度 1/5 1-3 小时 新手友好度 88/100
FROWNINGdev/django-orm-lens#268 ·
-
Finish the Chinese rule reference — DOL001–DOL007可能重新可做 @harbinresearcher 于 64 天前认领,目前没有进行中的 PR。 未关闭documentation good first issue help wanted
难度 2/5 1-3 小时 新手友好度 68/100
FROWNINGdev/django-orm-lens#81 · 2 条评论 ·
-
bug good first issue python
难度 3/5 半天 新手友好度 78/100
FROWNINGdev/django-orm-lens#266 ·
-
enhancement help wanted python
难度 4/5 3-5 天 新手友好度 35/100
FROWNINGdev/django-orm-lens#254 ·
查看 FROWNINGdev/django-orm-lens 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
gorakhargosh/watchdog#1462 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 85/100
openwisp/openwisp-monitoring#889 ·
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
OpenFreeEnergy/Lomap#180 ·
-
难度 2/5 1-3 小时 新手友好度 62/100
roman-corgi/corgisim#327 ·
维护者通常 2 天内回复
-
agent:WSL bug code-review linux pri:medium sev:low tooling
难度 2/5 半天 新手友好度 62/100
维护者通常 1 天内回复