Skip to content

Commit

Permalink
Add ManyToManyRelatedManager to auth models (#235)
Browse files Browse the repository at this point in the history
Adds managers for reverse names of many to many relationships in the contrib.auth app.

This is a small QoL improvement PR. It provides type checkers with the reverse relationships between:

- Group and Permission: Permission has a typed `.group_set` manager object
- PermissionMixin and Group: Group has a typed `.user_set` manager object

The reasons for using _PermissionMixin_ rather than other possible models/mixins are (1) the relationship is defined in the mixin to begin with, and (2) this is compatible with any model that inherits the mixin - e.g. AbstractUser, User, or a custom user model.

<img width="759" alt="Screenshot 2024-04-19 at 10 34 19 AM" src="https://github.com/sbdchd/django-types/assets/4360430/40dc3ce6-fd5a-4c7f-8318-5bbff80c5b9f">
  • Loading branch information
vforgione authored Apr 19, 2024
1 parent 01e4eb5 commit f0f1026
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion django-stubs/contrib/auth/models.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ from django.contrib.auth.validators import UnicodeUsernameValidator
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.db.models.base import Model
from django.db.models.manager import EmptyManager
from django.db.models.manager import EmptyManager, ManyToManyRelatedManager

_AnyUser = Model | AnonymousUser

Expand All @@ -34,6 +34,7 @@ class Permission(models.Model):
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
codename = models.CharField(max_length=100)
def natural_key(self) -> tuple[str, str, str]: ...
group_set = ManyToManyRelatedManager["Group", "Permission"]()

_GroupT = TypeVar("_GroupT", bound=Group)

Expand All @@ -46,6 +47,7 @@ class Group(models.Model):
name = models.CharField(max_length=150)
permissions = models.ManyToManyField[Permission, Any](Permission)
def natural_key(self) -> tuple[str, ...]: ...
user_set = ManyToManyRelatedManager["PermissionsMixin", "Group"]()

class UserManager(BaseUserManager[_T]):
def create_user(
Expand Down

0 comments on commit f0f1026

Please sign in to comment.