Python django.db.connection.queries() Examples

The following are 30 code examples of django.db.connection.queries(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.db.connection , or try the search function .
Example #1
Source File: test_api.py    From dynamic-rest with MIT License 6 votes vote down vote up
def test_get_with_nested_has_many(self):
        with self.assertNumQueries(2):
            # 2 queries: 1 for User, 1 for Group
            response = self.client.get('/users/?include[]=groups.')
        self.assertEquals(200, response.status_code)
        self.assertEquals(
            {'groups': [{'id': 1, 'name': '0'}, {'id': 2, 'name': '1'}],
             'users': [{
                 'groups': [1, 2], 'id': 1, 'location': 1, 'name': '0'
             }, {
                 'groups': [1, 2], 'id': 2, 'location': 1, 'name': '1'
             }, {
                 'groups': [1, 2], 'id': 3, 'location': 2, 'name': '2'
             }, {
                 'groups': [1, 2], 'id': 4, 'location': 3, 'name': '3'
             }]},
            json.loads(response.content.decode('utf-8'))) 
Example #2
Source File: test_api.py    From dynamic-rest with MIT License 6 votes vote down vote up
def test_get_with_exclude(self):
        with self.assertNumQueries(1):
            response = self.client.get('/users/?exclude[]=name')
        query = connection.queries[-1]['sql']
        self.assertFalse('name' in query, query)
        self.assertFalse('*' in query, query)

        self.assertEquals(200, response.status_code)
        self.assertEquals({
            'users': [{
                'id': 1,
                'location': 1
            }, {
                'id': 2,
                'location': 1
            }, {
                'id': 3,
                'location': 2
            }, {
                'id': 4,
                'location': 3
            }]
        }, json.loads(response.content.decode('utf-8'))) 
Example #3
Source File: api.py    From donation-tracker with Apache License 2.0 6 votes vote down vote up
def command(request):
    data = json.loads(request.POST.get('data', '{}'))
    func = getattr(commands, data['command'], None)
    if func:
        if request.user.has_perm(func.permission):
            output, status = func(data)
            output = serializers.serialize('json', output, ensure_ascii=False)
        else:
            output = json.dumps({'error': 'permission denied'})
            status = 403
    else:
        output = json.dumps({'error': 'unrecognized command'})
        status = 400
    resp = HttpResponse(output, content_type='application/json;charset=utf-8')
    if 'queries' in request.GET and request.user.has_perm('tracker.view_queries'):
        return HttpResponse(
            json.dumps(connection.queries, ensure_ascii=False, indent=1),
            status=status,
            content_type='application/json;charset=utf-8',
        )
    return resp 
Example #4
Source File: api.py    From donation-tracker with Apache License 2.0 6 votes vote down vote up
def me(request):
    if request.user.is_anonymous or not request.user.is_active:
        raise PermissionDenied
    output = {'username': request.user.username}
    if request.user.is_superuser:
        output['superuser'] = True
    else:
        permissions = request.user.get_all_permissions()
        if permissions:
            output['permissions'] = list(permissions)
    if request.user.is_staff:
        output['staff'] = True
    resp = HttpResponse(
        json.dumps(output), content_type='application/json;charset=utf-8'
    )
    if 'queries' in request.GET and request.user.has_perm('tracker.view_queries'):
        return HttpResponse(
            json.dumps(connection.queries, ensure_ascii=False, indent=1),
            status=200,
            content_type='application/json;charset=utf-8',
        )
    return resp 
Example #5
Source File: test_node.py    From django-business-logic with MIT License 6 votes vote down vote up
def test_medium_tree_cache(self):
        queries = connection.queries
        cache_holder = NodeCacheHolder()
        node = symmetric_tree(count=4)
        lft_mul_node, rgh_mul_node = node.get_children().all()
        lft_lft_child, rgh_lft_child = lft_mul_node.get_children().all()
        lft_rgh_child, rgh_rgh_child = rgh_mul_node.get_children().all()
        cached_lft_mul_node, cached_rgh_mul_node = cache_holder.get_children(node)

        max_num_queries = len(queries)
        cached_lft_lft_child, cached_rgh_lft_child = cache_holder.get_children(cached_lft_mul_node)
        cached_lft_rgh_child, cached_rgh_rgh_child = cache_holder.get_children(cached_rgh_mul_node)
        self.assertEqual(max_num_queries, len(queries))
        cached_lft_rgh_child, cached_rgh_rgh_child = cache_holder.get_children(rgh_mul_node)
        content_object = cached_rgh_mul_node.content_object
        content_object = cached_lft_rgh_child.content_object
        content_object = cached_rgh_rgh_child.content_object
        self.assertEqual(max_num_queries, len(queries)) 
Example #6
Source File: test_node.py    From django-business-logic with MIT License 6 votes vote down vote up
def test_simple_tree_cache(self):
        queries = connection.queries
        cache_holder = NodeCacheHolder()
        node = symmetric_tree()
        not_cached_children = node.get_children().all()
        cached_children = cache_holder.get_children(node)
        self.assertTrue(isinstance(cache_holder._node_cache, NodeCache))
        for i, child in enumerate(not_cached_children):
            self.assertEqual(child.id, cached_children[i].id)
        max_num_queries = len(queries)
        self.assertEqual(max_num_queries, len(queries))
        content_object = node.content_object
        self.assertEqual(max_num_queries, len(queries))
        content_object = cached_children[0].content_object
        self.assertEqual(max_num_queries, len(queries))
        content_object = cached_children[1].content_object
        self.assertEqual(max_num_queries, len(queries)) 
Example #7
Source File: test_api.py    From dynamic-rest with MIT License 6 votes vote down vote up
def test_get_with_nested_include(self):
        with self.assertNumQueries(3):
            # 3 queries: 1 for User, 1 for Group, 1 for Permissions
            response = self.client.get('/users/?include[]=groups.permissions')
        self.assertEquals(200, response.status_code)
        self.assertEquals(
            {'groups': [{'id': 1, 'name': '0', 'permissions': [1]},
                        {'id': 2, 'name': '1', 'permissions': [2]}],
             'users': [{
                 'groups': [1, 2], 'id': 1, 'location': 1, 'name': '0'
             }, {
                 'groups': [1, 2], 'id': 2, 'location': 1, 'name': '1'
             }, {
                 'groups': [1, 2], 'id': 3, 'location': 2, 'name': '2'
             }, {
                 'groups': [1, 2], 'id': 4, 'location': 3, 'name': '3'
             }
            ]},
            json.loads(response.content.decode('utf-8'))) 
Example #8
Source File: test_api.py    From dynamic-rest with MIT License 6 votes vote down vote up
def test_get_with_nested_exclude(self):
        with self.assertNumQueries(2):
            # 2 queries: 1 for User, 1 for Group
            response = self.client.get('/users/?exclude[]=groups.name')
        self.assertEquals(200, response.status_code)
        self.assertEquals(
            {'groups': [{'id': 1}, {'id': 2}],
             'users': [{
                 'groups': [1, 2], 'id': 1, 'location': 1, 'name': '0'
             }, {
                 'groups': [1, 2], 'id': 2, 'location': 1, 'name': '1'
             }, {
                 'groups': [1, 2], 'id': 3, 'location': 2, 'name': '2'
             }, {
                 'groups': [1, 2], 'id': 4, 'location': 3, 'name': '3'
             }]},
            json.loads(response.content.decode('utf-8'))) 
Example #9
Source File: decorators.py    From django-tip-02 with MIT License 6 votes vote down vote up
def debugger_queries(func):
    """Basic function to debug queries."""
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        print("func: ", func.__name__)
        reset_queries()

        start = time.time()
        start_queries = len(connection.queries)

        result = func(*args, **kwargs)

        end = time.time()
        end_queries = len(connection.queries)

        print("queries:", end_queries - start_queries)
        print("took: %.2fs" % (end - start))
        return result

    return wrapper 
Example #10
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_traverse_GFK(self):
        """
        A 'content_object' can be traversed with prefetch_related() and
        get to related objects on the other side (assuming it is suitably
        filtered)
        """
        TaggedItem.objects.create(tag="awesome", content_object=self.book1)
        TaggedItem.objects.create(tag="awesome", content_object=self.book2)
        TaggedItem.objects.create(tag="awesome", content_object=self.book3)
        TaggedItem.objects.create(tag="awesome", content_object=self.reader1)
        TaggedItem.objects.create(tag="awesome", content_object=self.reader2)

        ct = ContentType.objects.get_for_model(Book)

        # We get 3 queries - 1 for main query, 1 for content_objects since they
        # all use the same table, and 1 for the 'read_by' relation.
        with self.assertNumQueries(3):
            # If we limit to books, we know that they will have 'read_by'
            # attributes, so the following makes sense:
            qs = TaggedItem.objects.filter(content_type=ct, tag='awesome').prefetch_related('content_object__read_by')
            readers_of_awesome_books = {r.name for tag in qs
                                        for r in tag.content_object.read_by.all()}
            self.assertEqual(readers_of_awesome_books, {"me", "you", "someone"}) 
Example #11
Source File: test_api.py    From dynamic-rest with MIT License 6 votes vote down vote up
def test_get_with_filter_and_include_relationship(self):
        url = '/users/?include[]=groups.&filter{groups|name}=1'
        with self.assertNumQueries(2):
            # 2 queries: 1 for User, 1 for Group
            response = self.client.get(url)
        self.assertEquals(200, response.status_code)
        self.assertEquals(
            {
                'groups': [{'id': 2, 'name': '1'}],
                'users': [
                    {'groups': [2], 'id': 1, 'location': 1, 'name': '0'},
                    {'groups': [2], 'id': 2, 'location': 1, 'name': '1'},
                    {'groups': [2], 'id': 3, 'location': 2, 'name': '2'},
                    {'groups': [2], 'id': 4, 'location': 3, 'name': '3'}
                ]
            },
            json.loads(response.content.decode('utf-8'))) 
Example #12
Source File: sql_printing_middleware.py    From substra-backend with Apache License 2.0 6 votes vote down vote up
def __call__(self, request):
        response = self.get_response(request)

        if (len(connection.queries) == 0 or
                request.path_info.startswith('/favicon.ico') or
                request.path_info.startswith(settings.STATIC_URL) or
                request.path_info.startswith(settings.MEDIA_URL)):
            return response

        indentation = 2
        print(("\n\n%s\033[1;35m[SQL Queries for]\033[1;34m %s\033[0m\n" % (" " * indentation, request.path_info)))
        total_time = 0.0
        for query in connection.queries:
            if query['sql']:
                nice_sql = query['sql'].replace('"', '').replace(',', ', ')
                sql = "\033[1;31m[%s]\033[0m %s" % (query['time'], nice_sql)
                total_time = total_time + float(query['time'])
                print(("%s%s\n" % (" " * indentation, sql)))
        replace_tuple = (" " * indentation, str(total_time), str(len(connection.queries)))
        print(("%s\033[1;32m[TOTAL TIME: %s seconds (%s queries)]\033[0m" % replace_tuple))
        return response 
Example #13
Source File: logging_middleware.py    From jeeves with MIT License 5 votes vote down vote up
def process_response(self, request, response):
        logger.info("%s \"%s\" (%s)" % (request.method, smart_str(request.path_info), response.status_code))

        indentation = 2
        if len(connection.queries) > 0:
            total_time = 0.0
            for query in connection.queries:
                nice_sql = query['sql'].replace('"', '').replace(',',', ')
                sql = "[%s] %s" % (query['time'], nice_sql)
                total_time = total_time + float(query['time'])
                logger.info("%s%s\n" % (" "*indentation, sql))
            replace_tuple = (" "*indentation, str(total_time))
            logger.info("%s[TOTAL QUERIES: %d]" % (" "*indentation, len(connection.queries)))
            logger.info("%s[TOTAL TIME: %s seconds]" % replace_tuple)
        return response 
Example #14
Source File: logging_middleware.py    From jeeves with MIT License 5 votes vote down vote up
def process_response(self, request, response):
        logger.info("%s \"%s\" (%s)" % (request.method, smart_str(request.path_info), response.status_code))

        indentation = 2
        if len(connection.queries) > 0:
            total_time = 0.0
            for query in connection.queries:
                nice_sql = query['sql'].replace('"', '').replace(',',', ')
                sql = "[%s] %s" % (query['time'], nice_sql)
                total_time = total_time + float(query['time'])
                logger.info("%s%s\n" % (" "*indentation, sql))
            replace_tuple = (" "*indentation, str(total_time))
            logger.info("%s[TOTAL QUERIES: %d]" % (" "*indentation, len(connection.queries)))
            logger.info("%s[TOTAL TIME: %s seconds]" % replace_tuple)
        return response 
Example #15
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_parameter_quoting(self):
        # The implementation of last_executed_queries isn't optimal. It's
        # worth testing that parameters are quoted (#14091).
        query = "SELECT %s"
        params = ["\"'\\"]
        connection.cursor().execute(query, params)
        # Note that the single quote is repeated
        substituted = "SELECT '\"''\\'"
        self.assertEqual(connection.queries[-1]['sql'], substituted) 
Example #16
Source File: logging_middleware.py    From jeeves with MIT License 5 votes vote down vote up
def process_response(self, request, response):
        logger.info("%s \"%s\" (%s)" % (request.method, smart_str(request.path_info), response.status_code))

        indentation = 2
        if len(connection.queries) > 0:
            total_time = 0.0
            for query in connection.queries:
                nice_sql = query['sql'].replace('"', '').replace(',',', ')
                sql = "[%s] %s" % (query['time'], nice_sql)
                total_time = total_time + float(query['time'])
                logger.info("%s%s\n" % (" "*indentation, sql))
            replace_tuple = (" "*indentation, str(total_time))
            logger.info("%s[TOTAL QUERIES: %d]" % (" "*indentation, len(connection.queries)))
            logger.info("%s[TOTAL TIME: %s seconds]" % replace_tuple)
        return response 
Example #17
Source File: debug.py    From django-treenode with MIT License 5 votes vote down vote up
def _get_queries():
        return len(connection.queries) 
Example #18
Source File: debug.py    From django-treenode with MIT License 5 votes vote down vote up
def __exit__(self, type_, value, traceback):
        queries = (debug_performance._get_queries() - self.__init_queries)
        timer = (debug_performance._get_timer() - self.__init_timer)
        if settings.DEBUG:
            message = '\r%sexecuted %s %s in %ss.' % (
                self.__message_prefix,
                queries,
                'query' if queries == 1 else 'queries',
                timer, )
            print(message) 
Example #19
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_no_interpolation(self):
        # This shouldn't raise an exception (#17158)
        query = "SELECT strftime('%Y', 'now');"
        connection.cursor().execute(query)
        self.assertEqual(connection.queries[-1]['sql'], query) 
Example #20
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_m2m_then_reverse_fk_object_ids(self):
        with CaptureQueriesContext(connection) as queries:
            list(Book.objects.prefetch_related('authors__addresses'))

        sql = queries[-1]['sql']
        self.assertWhereContains(sql, self.author1.name) 
Example #21
Source File: logging_middleware.py    From jeeves with MIT License 5 votes vote down vote up
def process_response(self, request, response):
        logger.info("%s \"%s\" (%s)" % (request.method, smart_str(request.path_info), response.status_code))

        indentation = 2
        if len(connection.queries) > 0:
            total_time = 0.0
            for query in connection.queries:
                nice_sql = query['sql'].replace('"', '').replace(',',', ')
                sql = "[%s] %s" % (query['time'], nice_sql)
                total_time = total_time + float(query['time'])
                logger.info("%s%s\n" % (" "*indentation, sql))
            replace_tuple = (" "*indentation, str(total_time))
            logger.info("%s[TOTAL QUERIES: %d]" % (" "*indentation, len(connection.queries)))
            logger.info("%s[TOTAL TIME: %s seconds]" % replace_tuple)
        return response 
Example #22
Source File: logging_middleware.py    From jeeves with MIT License 5 votes vote down vote up
def process_response(self, request, response):
        logger.info("%s \"%s\" (%s)" % (request.method, smart_str(request.path_info), response.status_code))

        indentation = 2
        if len(connection.queries) > 0:
            total_time = 0.0
            for query in connection.queries:
                nice_sql = query['sql'].replace('"', '').replace(',',', ')
                sql = "[%s] %s" % (query['time'], nice_sql)
                total_time = total_time + float(query['time'])
                logger.info("%s%s\n" % (" "*indentation, sql))
            replace_tuple = (" "*indentation, str(total_time))
            logger.info("%s[TOTAL QUERIES: %d]" % (" "*indentation, len(connection.queries)))
            logger.info("%s[TOTAL TIME: %s seconds]" % replace_tuple)
        return response 
Example #23
Source File: utils.py    From django-sae with Apache License 2.0 5 votes vote down vote up
def clear_sql():
    connection.queries = [] 
Example #24
Source File: utils.py    From django-sae with Apache License 2.0 5 votes vote down vote up
def print_sql():
    if connection.queries:
        time = sum([float(q['time']) for q in connection.queries])
        t = Template(
            "{{count}} quer{{count|pluralize:\"y,ies\"}} in {{time}} seconds:\n{% for sql in sqllog %}[{{forloop.counter}}] {{sql.time}}s: {{sql.sql|safe}}{% if not forloop.last %}\n{% endif %}{% endfor %}")
        print t.render(Context({'sqllog': connection.queries, 'count': len(connection.queries), 'time': time})) 
Example #25
Source File: test_utils.py    From django-sae with Apache License 2.0 5 votes vote down vote up
def test_clear_sql(self):
        clear_sql()
        self.assertFalse(connection.queries) 
Example #26
Source File: test_utils.py    From django-sae with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        query_mock = {'time': 1, 'sql': 'sql mock'}
        connection.queries = [query_mock] 
Example #27
Source File: test_mysql.py    From django-sqlserver with MIT License 5 votes vote down vote up
def test_auto_is_null_auto_config(self):
        query = 'set sql_auto_is_null = 0'
        connection.init_connection_state()
        last_query = connection.queries[-1]['sql'].lower()
        if connection.features.is_sql_auto_is_null_enabled:
            self.assertIn(query, last_query)
        else:
            self.assertNotIn(query, last_query) 
Example #28
Source File: tests.py    From django-sqlserver with MIT License 5 votes vote down vote up
def test_large_batch_mixed_efficiency(self):
        """
        Test inserting a large batch with objects having primary key set
        mixed together with objects without PK set.
        """
        with override_settings(DEBUG=True):
            connection.queries_log.clear()
            TwoFields.objects.bulk_create([
                TwoFields(id=i if i % 2 == 0 else None, f1=i, f2=i + 1)
                for i in range(100000, 101000)])
            self.assertLess(len(connection.queries), 10) 
Example #29
Source File: tests.py    From django-sqlserver with MIT License 5 votes vote down vote up
def test_large_batch_efficiency(self):
        with override_settings(DEBUG=True):
            connection.queries_log.clear()
            TwoFields.objects.bulk_create([
                TwoFields(f1=i, f2=i + 1) for i in range(0, 1001)
            ])
            self.assertLess(len(connection.queries), 10) 
Example #30
Source File: build_homology_models_zip.py    From protwis with Apache License 2.0 5 votes vote down vote up
def main_func(self, positions, iteration, count, lock):
        processor_id = round(self.processors*positions[0]/len(self.models_to_do))+1
        while count.value<len(self.models_to_do):
            with lock:
                if len(self.models_to_do)>count.value:
                    modelname,path = self.models_to_do[count.value]
                    count.value +=1
            start_import = time.time()
            start_connections = len(connection.queries)
            self.upload_to_db(modelname, path)
            mod_dir = path+modelname
            shutil.rmtree(mod_dir)
            # print("Done",modelname, time.time()-start_import,len(connection.queries)-start_connections)