Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#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)

fix.patch (2.6 KB ) - added by krnr 5 years ago.

Download all attachments as: .zip

Change History (5)

by krnr, 5 years ago

Attachment: fix.patch added

comment:1 by krnr, 5 years ago

Owner: changed from nobody to krnr
Status: newassigned

comment:2 by krnr, 5 years ago

Type: BugCleanup/optimization

comment:3 by Mariusz Felisiak, 5 years ago

Resolution: wontfix
Status: assignedclosed
Summary: UserAdmin returns incorrect fieldsets when model has overridden __bool__UserAdmin returns incorrect fieldsets when model has overridden __bool__.

I know that this fix is quite small but you will find other places where we use if instance/obj pattern, e.g. in SingleObjectMixin. 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.

in reply to:  3 comment:4 by krnr, 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.

Note: See TracTickets for help on using tickets.
Back to Top