Opened 4 years ago

Closed 4 years ago

#31266 closed New feature (wontfix)

Implement `user.is_member(group)` to easily check whether a user belongs to a group or not

Reported by: Martin Zugnoni Owned by: Martin Zugnoni
Component: contrib.auth Version: dev
Severity: Normal Keywords: auth, groups, users
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

While checking if a user is member of certain group, there's no easy way to check it other than going through the "groups" M2M relationship manually, like this.

> user.groups.filter(name="some-group").exists()
True

I propose to extend the django.contrib.auth.models.PermissionsMixin class, with a is_member(group) method, that returns True/False checking whether the user belongs to the group or not. Similar to the user.has_perm("some-permission"), but for groups.

The same operation using this new method would look like this:

> user.is_member("some-group")
True

I'm open to suggestions.

Change History (2)

comment:1 by Martin Zugnoni, 4 years ago

Version: 3.0master

comment:2 by Carlton Gibson, 4 years ago

Resolution: wontfix
Status: assignedclosed

There should be one-- and preferably only one --obvious way to do it.

Hi Martin. I'm going to say this isn't worth the extra API surface area.
(You can canvas the DevelopersMailingList if you wish but...)

Thanks.

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