Flip the many-to-many relationships for users & groups to object permissions #9369

Closed
opened 2025-12-29 20:49:03 +01:00 by adam · 0 comments
Owner

Originally created by @jeremystretch on GitHub (Mar 18, 2024).

Proposed Changes

There are currently many-to-many relationships from the ObjectPermission model to the User and Group models within the users app. These are both defined on the ObjectPermission model:

class ObjectPermission(models.Model):
    groups = models.ManyToManyField(
        to='users.Group',
        blank=True,
        related_name='object_permissions'
    )
    users = models.ManyToManyField(
        to=get_user_model(),
        blank=True,
        related_name='object_permissions'
    )

This proposal entails moving the primary M2M field definitions from ObjectPermission to the User and Group models, as such:

class Group(models.Model):
    object_permissions = models.ManyToManyField(
        to='users.ObjectPermission',
        blank=True,
        related_name='groups'
    )

class User(models.Model):
    object_permissions = models.ManyToManyField(
        to='users.ObjectPermission',
        blank=True,
        related_name='users'
    )

This is essentially a cosmetic change, as the related field names for all three models will remain the same. However, a migration will be required to effect the swap and renamed the intermediary tables accordingly. There may also be some side effects of the reversal that need to be handled; testing will be needed to identify them. (In the event any serious blockers arise, this initiative should probably be punted.)

Justification

Moving these relationships to the User & Group models feels more natural and less surprising than the current arrangement, which exists only because NetBox did not employ custom user or group models prior to v4.0.

Originally created by @jeremystretch on GitHub (Mar 18, 2024). ### Proposed Changes There are currently many-to-many relationships from the ObjectPermission model to the User and Group models within the `users` app. These are both defined on the ObjectPermission model: ```python class ObjectPermission(models.Model): groups = models.ManyToManyField( to='users.Group', blank=True, related_name='object_permissions' ) users = models.ManyToManyField( to=get_user_model(), blank=True, related_name='object_permissions' ) ``` This proposal entails moving the primary M2M field definitions from ObjectPermission to the User and Group models, as such: ```python class Group(models.Model): object_permissions = models.ManyToManyField( to='users.ObjectPermission', blank=True, related_name='groups' ) class User(models.Model): object_permissions = models.ManyToManyField( to='users.ObjectPermission', blank=True, related_name='users' ) ``` This is essentially a cosmetic change, as the related field names for all three models will remain the same. However, a migration will be required to effect the swap and renamed the intermediary tables accordingly. There may also be some side effects of the reversal that need to be handled; testing will be needed to identify them. (In the event any serious blockers arise, this initiative should probably be punted.) ### Justification Moving these relationships to the User & Group models feels more natural and less surprising than the current arrangement, which exists only because NetBox did not employ custom user or group models prior to v4.0.
adam added the status: acceptedtype: housekeeping labels 2025-12-29 20:49:03 +01:00
adam closed this issue 2025-12-29 20:49:03 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#9369