我正在使用django和django rest框架来对所有用户进行查询,这些数据具有作为url参数发送的权限,但此查询花费的时间太长。
我是用户pycharm调试器我怎样才能检查为什么它需要很长时间,这是函数:
@list_route(url_path='permission/(?P<permission>.+)')
def read_permission(self, request, *args, **kwargs):
serializer = self.get_serializer_class()
qs = get_user_model().objects.filter_by_permission(self.kwargs.get('permission'))
qs = qs.order_by(Lower('username'))
return Response(serializer(qs, many=True).data)
添加序列化程序
class UserSerializer(UserLabelMixin):
user_permissions = serializers.SlugRelatedField(many=True, read_only=True, slug_field='codename')
class Meta:
model = get_user_model()
fields = ['id', 'email', 'is_superuser', 'is_staff', 'label',
'full_name', 'first_name', 'last_name', 'username',
'teams', 'date_joined', 'last_login',
'user_permissions', 'groups', 'ui_preferences', 'internal_project',
'staff_id', 'oem_id', 'oem_email', 'oem_department', 'comment']
read_only_fields = fields
答案 0 :(得分:0)
这可能对您有所帮助
get_user_model().objects.prefetch_related("user_permissions", "groups").filter_by_permission...