How to create custom groups in django from group

I am trying to understand how to create custom groups in Django, so that the groups are related to the application, not Project.

For example, when a user wants to create a company, he must be the owner of the company:

model

class Company(models.Model): owner = models.ForeignKey(User) name = models.CharField(max_length=64, unique=True) description = models.TextField(max_length=512) created_on = models.DateTimeField(auto_now_add=timezone.now) class Meta: permissions = ( ("erp_view_company", "Can see the company information"), ("erp_edit_company", "Can edit the company information"), ("erp_delete_company", "Can delete the company"), ) 

View

 # Create your views here. def register(request, template_name='erp/register.html'): if request.method == 'POST': company_form = CompanyRegistrationForm(request.POST) if company_form.is_valid(): # Create a new company object but avoid saving it yet new_company = company_form.save(commit=False) # Set the owner if request.user.is_authenticated(): new_company.owner = request.user # Add the user to group admin group = Group.objects.get_or_create(name="erp_admin") request.user.groups.add(group) # Save the User object new_company.save() template_name = 'erp/register_done.html' return render(request, template_name, {'new_company': new_company}) else: company_form = CompanyRegistrationForm() return render(request, template_name, {'company_form': company_form}) 

Here are the questions:

1) Instead of Group.objects.get_or_create(name="erp_admin") I would like to have CompanyGroup.objects.get_or_create(name="admin") so that the application groups are limited by the application.

2) How can I match the permissions defined in the Meta class of each model to a group?

3) User groups are associated with the company, this means that each company has an Admin group, which begins with the owner. The owner can create the user as a "Manager". The manager can create groups / permissions, add users, set permissions for the user, but cannot in any case CRUD admin groups. Thus, there is a hierarchy in groups (I believe that the hierarchy depends on the rights added to the group).

To make this clearer, you can see a concept image: company example

So, I think the main problems are:

1) How to inherit groups to create custom groups.

2) How to match model permissions for a group.

3) How groups may be limited by CompanyID.

I hope the problem is well defined. Let me know if I clarify anything.

Thanks in advance for your support.

Change 1)

For point 3, I found this answer on SO: Extend django groups and permissions , but then the problem is how to request this data and how to check permissions, And how could you override the save () method to add meta information to the "name property "? The name of the group may be something like: "company_id # admin" or "company_id # manager".

+7
python inheritance django permissions usergroups
source share
1 answer

You can search for permissions on objects. Django does not support this out of the box, but it is possible using the Django-guardian application

0
source share

All Articles