可能更有效的替代品?

时间:2017-08-24 20:52:15

标签: python django

是否可以提高此功能的效率,并且可能更具可读性?

另外,我这样做了吗?我的意思是,它有效,但是做一个复杂的查询(带注释)或一个简单的查询然后用“纯python”代码块处理结果会更好吗?

以下是摘录:

def save_model(self, request, obj, form, change):
        #Creates a list of dictionaries with the group name and role name
        roles = [{'group': group.name, 'role': role.name} for group in form.cleaned_data['groups'] for role in group.groupextended.role.all()]
        roles = sorted(roles, key=lambda k: k['role'])
        #Groups by the role name
        for role, group in itertools.groupby(roles, key=lambda k: k['role']):
            group_list = list(group)
            #Check if the role is in more of one group associated to the user
            if len(group_list) > 1:
                raise Exception('Exception test: {0}, {1}, {2}'.format(role, obj.username, group_list))

        super(UserExtendedAdmin, self).save_model(request, obj, form, change)

由于

0 个答案:

没有答案