當前位置: 首頁>>代碼示例>>Python>>正文


Python sql.EmptyResultSet方法代碼示例

本文整理匯總了Python中django.db.models.sql.EmptyResultSet方法的典型用法代碼示例。如果您正苦於以下問題:Python sql.EmptyResultSet方法的具體用法?Python sql.EmptyResultSet怎麽用?Python sql.EmptyResultSet使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在django.db.models.sql的用法示例。


在下文中一共展示了sql.EmptyResultSet方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: using_bundle_cache

# 需要導入模塊: from django.db.models import sql [as 別名]
# 或者: from django.db.models.sql import EmptyResultSet [as 別名]
def using_bundle_cache(f, bundle, *args, **kwds):
    """Wraps a function that returns a queryset, and then caches the result in the bundle."""
    queryset = f(bundle, *args, **kwds)
    if hasattr(bundle, '_resource_cache'):
        # The SQL query itself is the key into the cache.
        try:
            # Sometimes this raises EmptyResultSet, so ignore the cache in that case
            # Know Django bug: https://code.djangoproject.com/ticket/22973
            # This occcured when cloning ConfigEntity, the toMany fields of config_entity_resource.py
            # encountered this error. Specifically, I belive it happened with our queries of the
            # toMany relationsip in permission_resource_mixin.py, but it may have been others as well.
            query = str(queryset.query)
        except EmptyResultSet:
            return queryset

        # Return the cached queryset rather than the one we just
        # made, because it has cached results in it.
        if query in bundle._resource_cache:
            bundle._cache_hits[query] += 1
            return bundle._resource_cache[query]

        bundle._resource_cache[query] = queryset
    return queryset 
開發者ID:CalthorpeAnalytics,項目名稱:urbanfootprint,代碼行數:25,代碼來源:caching.py

示例2: to_df

# 需要導入模塊: from django.db.models import sql [as 別名]
# 或者: from django.db.models.sql import EmptyResultSet [as 別名]
def to_df(queryset):
    """
    :param queryset: django.db.models.query.QuerySet
    :return: pandas.core.frame.DataFrame
    """
    try:
        query, params = queryset.query.sql_with_params()
    except EmptyResultSet:
        # Occurs when Django tries to create an expression for a
        # query which will certainly be empty
        # e.g. Book.objects.filter(author__in=[])
        return pd.DataFrame()
    return read_sql_query(query, connection, params=params) 
開發者ID:BigBrotherTrade,項目名稱:trader,代碼行數:15,代碼來源:models.py

示例3: iterator

# 需要導入模塊: from django.db.models import sql [as 別名]
# 或者: from django.db.models.sql import EmptyResultSet [as 別名]
def iterator(self):
        try:
            key = self.generate_key()
        # workaround for Django bug # 12717
        except EmptyResultSet:
            return
        result_set = self.cache_backend.get(key)
        if result_set is None:
            logger.debug('cache miss for key {0}'.format(key))
            result_set = list(super(CachingQuerySet, self).iterator())
            self.cache_backend.set(key, result_set)
        for result in result_set:
            yield result 
開發者ID:vijaykatam,項目名稱:django-cache-manager,代碼行數:15,代碼來源:cache_manager.py

示例4: test_empty_list_on_filter_in

# 需要導入模塊: from django.db.models import sql [as 別名]
# 或者: from django.db.models.sql import EmptyResultSet [as 別名]
def test_empty_list_on_filter_in(self):
        """
        A filter call with __in being passed an empty list should correctly
        handle the EmptyResultSet exception and return None.
        """
        self.assertEqual([], list(Car.objects.filter(make__in=[]))) 
開發者ID:vijaykatam,項目名稱:django-cache-manager,代碼行數:8,代碼來源:model_integration_tests.py

示例5: test_catch_empty_result_set

# 需要導入模塊: from django.db.models import sql [as 別名]
# 或者: from django.db.models.sql import EmptyResultSet [as 別名]
def test_catch_empty_result_set(self, mock_generate_key, mock_cache_backend, invalidate_model_cache):
        """
        When an EmptyResultSet exception is raised in the process
        of passing an empty iterable to an __in parameter, CacheManager
        should correctly handle it and return None.
        """
        mock_generate_key.side_effect = EmptyResultSet()
        manufacturers = Manufacturer.objects.filter(name__in=[])
        self.assertEqual([], list(manufacturers)) 
開發者ID:vijaykatam,項目名稱:django-cache-manager,代碼行數:11,代碼來源:cache_manager_tests.py

示例6: execute_sql

# 需要導入模塊: from django.db.models import sql [as 別名]
# 或者: from django.db.models.sql import EmptyResultSet [as 別名]
def execute_sql(self, *args, **kwargs):
    """wrapper around real execute_sql in order to extract information"""

    try:
        q, params = self.as_sql()
        if not q:
            raise EmptyResultSet
    except EmptyResultSet:
        try:
            result_type = args[0]
        except IndexError:
            result_type = kwargs.get('result_type', 'multi')
        if result_type == 'multi':
            return iter([])
        else:
            return
    tb = ''.join(reversed(traceback.format_stack()))
    sql_query = q % params
    if _should_wrap(sql_query):
        query_dict = {
            'query': sql_query,
            'start_time': timezone.now(),
            'traceback': tb
        }
        try:
            return self._execute_sql(*args, **kwargs)
        finally:
            query_dict['end_time'] = timezone.now()
            request = DataCollector().request
            if request:
                query_dict['request'] = request
            if self.query.model.__module__ != 'silk.models':
                DataCollector().register_query(query_dict)
            else:
                DataCollector().register_silk_query(query_dict)
    return self._execute_sql(*args, **kwargs) 
開發者ID:jazzband,項目名稱:django-silk,代碼行數:38,代碼來源:sql.py


注:本文中的django.db.models.sql.EmptyResultSet方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。