You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is my code for create room and generate group permissions:
fromguardian.shortcutsimportassign_permclassCreateRoomView(generics.CreateAPIView):
def_get_permission_string(self, perm):
""" Get string permission, the return is like 'room.add_room' """returnf'room.{perm.codename}'defpost(self):
# Create a roombody= {}
room=Room.objects.create(**body)
# Get content typectype=ContentType.objects.get_for_model(Room)
# Define name of owner and staff group for this room permissionowner_group_name=_('Room Owner#room#{pk}')
staff_group_name=_('Room Staff#room#{pk}')
# Create groupowner_group=Group.objects.create(
name=owner_group_name.format(pk=room.pk))
staff_group=Group.objects.create(
name=staff_group_name.format(pk=room.pk))
# Get permissions by group permissionsowner_perms=Permission.objects.filter(
codename__in=Room.PERMISSION_GROUPS.get('owner'), content_type=ctype)
staff_perms=Permission.objects.filter(
codename__in=Room.PERMISSION_GROUPS.get('staff'), content_type=ctype)
# Assign owner permissions to the groupforowner_perminowner_perms:
perm=self._get_permission_string(owner_perm)
assign_perm(perm, owner_group, obj=room)
# Assign staff permissions to the groupforstaff_perminstaff_perms:
perm=self._get_permission_string(staff_perm)
assign_perm(perm, staff_group, obj=room)
# Assign owner group permission to the userrequest.user.groups.add(owner_group)
When I look at the query logs, there are a lot of queries being performed in this process.
Actually there are still a few groups and the assignment process as above, but I only included a few examples, so there are not too many.If calculated, there may be more than 50 query executions that run in this process.
So, how can I optimize the query for this problem? Can anyone share how to do it better.
Thank you,
The text was updated successfully, but these errors were encountered:
You can pass instances of Permission to assign_perm. perm = self._get_permission_string(owner_perm) leads to an additional query per permission.
If this isn't enough you can assign the permissions in bulk, see code below. But be aware that I didn't test this code. It might need adjustments, it might not work at all.
Before:
# Assign owner permissions to the groupforowner_perminowner_perms:
perm=self._get_permission_string(owner_perm)
assign_perm(perm, owner_group, obj=room)
# Assign staff permissions to the groupforstaff_perminstaff_perms:
perm=self._get_permission_string(staff_perm)
assign_perm(perm, staff_group, obj=room)
After:
# RoomGroupObjectPermission is the model from suggestion 1, use direct foreign keysroom_group_object_permissions= [
RoomGroupObjectPermission(
permission=owner_perm, group=owner_group, content_object=room,
)
forowner_perminowner_perms
] + [
RoomGroupObjectPermission(
permission=staff_perm, group=staff_group, content_object=room,
)
forstaff_perminstaff_perms
]
RoomGroupObjectPermission.objects.bulk_create(room_group_object_permissions)
I'm currently working on a project where one room has its own group permissions.
When someone creates a new room, the system will do the following:
Room Owner#room#{pk}
Here is my Room model :
This is my code for create room and generate group permissions:
When I look at the query logs, there are a lot of queries being performed in this process.
Actually there are still a few groups and the assignment process as above, but I only included a few examples, so there are not too many.If calculated, there may be more than 50 query executions that run in this process.
So, how can I optimize the query for this problem? Can anyone share how to do it better.
Thank you,
The text was updated successfully, but these errors were encountered: