Is there a way to change the functionality of the base Django Group model?
I have a Django app with the following architecture; a Company model that holds multiple users (the user is a custom user extending AbstractUser). Each user can only be a member of one Company. We are using the base Django Group model and the base Django permissions as well.
We extended the user model, so we can add the company field to it, and make the username unique per company, instead of unique globally. This allows us to have multiple users with the same username, as long as they are not a part of the same company.
We want to do the same thing with the groups; be able to have multiple groups with the same name, as long as they are not tied to the same company.
At one point, we extended the group model, by inheriting from the base Group model, and added the company field to it, which creates a one to one relationship between our CustomGroup and the base Group. This all works fine. The issue is we cannot find a way to change the logic of the Group model, so that the company and name field are unique together, as opposed to the name being unique.
This is our CustomGroup model:
class CustomGroup(Group):
company = models.ForeignKey(Company, on_delete=models.CASCADE, blank=True, null=True)
hidden = models.BooleanField(default=False)
class Meta:
indexes = [models.Index(fields=['company'])]
Any ideas on how to achieve this would be appreciated. Thank you.