#31472 closed Cleanup/optimization (wontfix)
UserAdmin returns incorrect fieldsets when model has overridden __bool__.
Reported by: | krnr | Owned by: | krnr |
---|---|---|---|
Component: | contrib.auth | Version: | dev |
Severity: | Normal | Keywords: | UserAdmin, add_fieldsets, get_fieldsets |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
in cases when model has "soft-delete" fields and defined __bool__
like these:
class CustomUser(models.User): is_deleted = models.BooleanField(default=False) def __bool__(self): return not self.is_deleted
the UserAdmin
will use add_fieldsets
because of comparison in get_fieldsets
.
I have a simple fix for this: just check against None
instead of boolean evaluation.
Attachments (1)
Change History (5)
by , 5 years ago
comment:1 by , 5 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:2 by , 5 years ago
Type: | Bug → Cleanup/optimization |
---|
follow-up: 4 comment:3 by , 5 years ago
Resolution: | → wontfix |
---|---|
Status: | assigned → closed |
Summary: | UserAdmin returns incorrect fieldsets when model has overridden __bool__ → UserAdmin returns incorrect fieldsets when model has overridden __bool__. |
comment:4 by , 5 years ago
I was fine with the existing admin class (because I seldom meet such user models), but recently I saw this PR: https://github.com/django/django/commit/5cd4c3e5595128bc1a3f28f2e30bab2e4dd3b1b7 and I was thinking "why does it have nice check against None in one method and doesn't have in another?"
and it's not about checking against all possible if instance
(yeah, it doesn't worth it), just about fieldsets.
but that's OK. just leave it be if anyone else will be curious.
I know that this fix is quite small but you will find other places where we use
if instance/obj
pattern, e.g. inSingleObjectMixin
. Defining__bool__
on a model instance is an edge case and your issue can be worked around by defining and using e.g.is_active(self): return not self.is_deleted
. See also arguments for closing a similar ticket.