Python django.db.models.QuerySet() Examples

The following are 30 code examples of django.db.models.QuerySet(). 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.models , or try the search function .
Example #1
Source File: features.py    From lexpredict-contraxsuite with GNU Affero General Public License v3.0 6 votes vote down vote up
def __init__(self,
                 queryset: Optional[Union[QuerySet, List[Document]]] = None,
                 project_id: Optional[int] = None,
                 project_name_filter: Optional[str] = None,
                 unit_type: str = 'sentence',
                 feature_source: str = 'term',
                 drop_empty_rows=True,
                 drop_empty_columns=True,
                 external_feature_names=None,
                 log_message: Callable[[str, str], None] = None):
        super().__init__(queryset, project_id, project_name_filter,
                         unit_type, feature_source,
                         drop_empty_rows, drop_empty_columns,
                         external_feature_names, log_message)
        self.transformer = None  # type: Optional[BaseTransformer]
        self.project_ids = []  # type: List[str] 
Example #2
Source File: generics.py    From djangochannelsrestframework with MIT License 6 votes vote down vote up
def get_queryset(self, **kwargs) -> QuerySet:
        """
        Get the list of items for this view.
        This must be an iterable, and may be a queryset.
        Defaults to using `self.queryset`.

        This method should always be used rather than accessing `self.queryset`
        directly, as `self.queryset` gets evaluated only once, and those results
        are cached for all subsequent requests.

        You may want to override this if you need to provide different
        querysets depending on the incoming request.

        (Eg. return a list of items that is specific to the user)
        """
        assert self.queryset is not None, (
            "'%s' should either include a `queryset` attribute, "
            "or override the `get_queryset()` method." % self.__class__.__name__
        )

        queryset = self.queryset
        if isinstance(queryset, QuerySet):
            # Ensure queryset is re-evaluated on each request.
            queryset = queryset.all()
        return queryset 
Example #3
Source File: backends.py    From lego with MIT License 6 votes vote down vote up
def has_perm(self, user_obj, perm, obj=None):
        if not user_obj.is_anonymous and not user_obj.is_active:
            return False

        if obj is None:
            # Take a shortcut and check KeywordPermissions only if no object are defined.
            return KeywordPermissions.has_perm(user_obj, perm)

        if isinstance(obj, models.Model):
            permission_handler = get_permission_handler(obj)
            return permission_handler.has_perm(user_obj, perm, obj=obj)
        elif isinstance(obj, models.QuerySet):
            permission_handler = get_permission_handler(obj.model)
            return permission_handler.has_perm(user_obj, perm, queryset=obj)
        elif issubclass(obj, models.Model):
            permission_handler = get_permission_handler(obj)
            return permission_handler.has_perm(
                user_obj, perm, queryset=obj.objects.none()
            )

        return False 
Example #4
Source File: prefetch.py    From dynamic-rest with MIT License 6 votes vote down vote up
def _get_django_queryset(self):
        """Return Django QuerySet with prefetches properly configured."""

        prefetches = []
        for field, fprefetch in self.prefetches.items():
            has_query = hasattr(fprefetch, 'query')
            qs = fprefetch.query.queryset if has_query else None
            prefetches.append(
                Prefetch(field, queryset=qs)
            )

        queryset = self.queryset
        if prefetches:
            queryset = queryset.prefetch_related(*prefetches)

        return queryset 
Example #5
Source File: tasks.py    From karrot-backend with GNU Affero General Public License v3.0 6 votes vote down vote up
def daily_activity_notifications():

    with timer() as t:
        for group in Group.objects.all():
            with timezone.override(group.timezone):
                if timezone.localtime().hour != 20:  # only at 8pm local time
                    continue

                for data in fetch_activity_notification_data_for_group(group):
                    prepare_activity_notification_email(**data).send()
                    stats.activity_notification_email(
                        group=data['group'], **{k: v.count()
                                                for k, v in data.items() if isinstance(v, QuerySet)}
                    )

    stats_utils.periodic_task('activities__daily_activity_notifications', seconds=t.elapsed_seconds) 
Example #6
Source File: prefetch.py    From dynamic-rest with MIT License 6 votes vote down vote up
def __getitem__(self, k):
        """Support list index and slicing, similar to Django QuerySet."""

        if self._data is not None:
            # Query has already been executed. Extract from local cache.
            return self._data[k]

        # Query hasn't yet been executed. Update queryset.
        if isinstance(k, slice):
            if k.start is not None:
                start = int(k.start)
            else:
                start = None
            if k.stop is not None:
                stop = int(k.stop)
            else:
                stop = None
            if k.step:
                raise TypeError("Stepping not supported")

            self.queryset.query.set_limits(start, stop)
            return self.execute()
        else:
            self.queryset.query.set_limits(k, k+1)
            return self.execute() 
Example #7
Source File: utils.py    From django-cachalot with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _find_subqueries_in_where(children):
    for child in children:
        child_class = child.__class__
        if child_class is WhereNode:
            for grand_child in _find_subqueries_in_where(child.children):
                yield grand_child
        elif child_class is ExtraWhere:
            raise IsRawQuery
        elif child_class is NothingNode:
            pass
        else:
            rhs = child.rhs
            rhs_class = rhs.__class__
            if rhs_class is Query:
                yield rhs
            elif rhs_class is QuerySet:
                yield rhs.query
            elif rhs_class is Subquery or rhs_class is Exists:
                try:
                    yield rhs.query
                except:
                    yield rhs.queryset.query
            elif rhs_class in UNCACHABLE_FUNCS:
                raise UncachableQuery 
Example #8
Source File: api.py    From django-carrot with Apache License 2.0 6 votes vote down vote up
def get_queryset(self) -> QuerySet:
        """
        Returns a queryset of `carrot.models.MessageLog` objects. If a `search_term` is provided in the request query
        params, then the result is filtered based on this. If using postgres, this is done using SearchVectors for
        improved performance
        """
        search_term = self.request.query_params.get('search', None)
        qs = self.queryset.all()
        if search_term:
            if settings.DATABASES.get('default', {}).get('ENGINE') == 'django.db.backends.postgresql_psycopg2':
                qs = qs.annotate(search=SearchVector('task', 'content', 'task_args')).filter(search=search_term)
            else:
                qs = (
                    qs.filter(task__icontains=search_term) |
                    qs.filter(content__icontains=search_term) |
                    qs.filter(task_args__icontains=search_term)
                ).distinct()

        return qs 
Example #9
Source File: models.py    From zing with GNU General Public License v3.0 6 votes vote down vote up
def _earliest_or_latest(self, field_name=None, direction="-"):
        """
        Overrides QuerySet._earliest_or_latest to add pk for secondary ordering
        """
        order_by = field_name or getattr(self.model._meta, "get_latest_by")
        assert bool(order_by), (
            "earliest() and latest() require either a "
            "field_name parameter or 'get_latest_by' in the model"
        )
        assert (
            self.query.can_filter()
        ), "Cannot change a query once a slice has been taken."
        obj = self._clone()
        obj.query.set_limits(high=1)
        obj.query.clear_ordering(force_empty=True)
        # add pk as secondary ordering for Submissions
        obj.query.add_ordering(
            "%s%s" % (direction, order_by), "%s%s" % (direction, "pk")
        )
        return obj.get() 
Example #10
Source File: models.py    From django-actions-logger with MIT License 6 votes vote down vote up
def get_for_objects(self, queryset):
        """
        Get log entries for the objects in the specified queryset.
        :param queryset: The queryset to get the log entries for.
        :type queryset: QuerySet
        :return: The LogAction objects for the objects in the given queryset.
        :rtype: QuerySet
        """
        if not isinstance(queryset, QuerySet) or queryset.count() == 0:
            return self.none()

        content_type = ContentType.objects.get_for_model(queryset.model)
        primary_keys = queryset.values_list(queryset.model._meta.pk.name, flat=True)

        if isinstance(primary_keys[0], integer_types):
            return self.filter(content_type=content_type).filter(Q(object_id__in=primary_keys)).distinct()
        else:
            return self.filter(content_type=content_type).filter(Q(object_pk__in=primary_keys)).distinct() 
Example #11
Source File: models.py    From lexpredict-contraxsuite with GNU Affero General Public License v3.0 6 votes vote down vote up
def drop_clusters(self, exclude_task_ids: Set = None, exclude_project_clustering_id: int = None):
        project = self
        # Stop running tusks
        from apps.task.tasks import purge_task
        from apps.project.tasks import ClusterProjectDocuments
        task_qr = project.project_tasks \
            .filter(name=ClusterProjectDocuments.name, status__in=UNREADY_STATES)  # type: QuerySet
        if exclude_task_ids:
            task_qr = task_qr.exclude(pk__in=exclude_task_ids)

        for task in task_qr:
            purge_task(task.pk, wait=True, timeout=1.5)
        # delete DocumentClusters
        for pcl in project.projectclustering_set.all():
            pcl.document_clusters.all().delete()
        # delete ProjectClustering
        project.projectclustering_set.exclude(id=exclude_project_clustering_id).delete()
        # delete ClusterProjectDocuments Tasks
        to_delete_qr = project.project_tasks.filter(name=ClusterProjectDocuments.name,
                                                    status__in=[SUCCESS, PENDING])  # type: QuerySet
        if exclude_task_ids:
            to_delete_qr = to_delete_qr.exclude(pk__in=exclude_task_ids)
        to_delete_qr.delete() 
Example #12
Source File: models.py    From lexpredict-contraxsuite with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_depends_on_uids(self) -> QuerySet:
        return self.depends_on_fields.all().values_list('pk', flat=True) 
Example #13
Source File: model_anonymizers.py    From django-GDPR with MIT License 5 votes vote down vote up
def get_reversion_versions(self, obj: Model):
        from gdpr.utils import get_reversion_versions
        versions = [i for i in get_reversion_versions(obj)]  # QuerySet to list
        parent_obj_versions = [get_reversion_versions(i) for i in self.get_all_parent_objects(obj)]
        versions += [item for sublist in parent_obj_versions for item in sublist]
        return versions 
Example #14
Source File: test_regexps_field_detection.py    From lexpredict-contraxsuite with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_field_detectors(self, field: DocumentField) -> \
            Union[QuerySet, List[DocumentFieldDetector]]:
        return self.detectors 
Example #15
Source File: test_regexps_field_detection.py    From lexpredict-contraxsuite with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_doc_text_units(self, doc: Document, text_unit_type: str) -> \
            Union[QuerySet, List[TextUnit]]:
        #return [u for u in self.units if u.document == doc and
        #        u.unit_type == text_unit_type]
        return self.units 
Example #16
Source File: base_field_detector_repository.py    From lexpredict-contraxsuite with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_field_detectors(self, field: DocumentField) -> \
            Union[QuerySet, List[DocumentFieldDetector]]:
        raise NotImplementedError() 
Example #17
Source File: text_unit_repository.py    From lexpredict-contraxsuite with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_doc_text_units(self, doc: Document, text_unit_type: str) -> \
            Union[QuerySet, List[TextUnit]]:
        return TextUnit.objects.all() \
            .filter(document=doc) \
            .filter(unit_type=text_unit_type) \
            .select_related('textunittext') \
            .order_by('location_start', 'pk') \
            .defer('textunittext__text_tsvector') 
Example #18
Source File: field_detection_repository.py    From lexpredict-contraxsuite with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_qs_active_modified_document_ids(self, field: DocumentField,
                                            project_ids: Optional[List[str]]) -> QuerySet:
        q = FieldValue.objects \
            .filter(field_id=field.pk, document__status__is_active=True, modified_by__isnull=False)
        if project_ids:
            q = q.filter(document__project_id__in=project_ids)
        return q.values_list('document_id', flat=True) 
Example #19
Source File: models.py    From lexpredict-contraxsuite with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_invalid_choice_annotations(self) -> 'Union[QuerySet, List[FieldAnnotation]]':
        if not self.allow_values_not_specified_in_choices:
            return FieldAnnotation.objects \
                .filter(field=self) \
                .exclude(value__in=self.get_choice_values())
        return FieldAnnotation.objects.none() 
Example #20
Source File: models.py    From lexpredict-contraxsuite with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_depends_on_codes(self) -> QuerySet:
        return self.depends_on_fields.all().values_list('code', flat=True) 
Example #21
Source File: document_field_repository.py    From lexpredict-contraxsuite with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_field_values_by_ids(self, ids: Iterable[int]) -> Union[QuerySet, List[FieldAnnotation]]:
        qs = FieldValue.objects \
            .filter(pk__in=ids) \
            .select_related('field')
        return qs 
Example #22
Source File: admin.py    From lexpredict-contraxsuite with GNU Affero General Public License v3.0 5 votes vote down vote up
def filter_count_predicate(self, qs: QuerySet) -> CustomCountQuerySet:
        inner_query = stringify_queryset(qs)
        qs = CustomCountQuerySet.wrap(qs)  # type: CustomCountQuerySet
        full_query = f'SELECT COUNT(text_unit_id) FROM ({inner_query} LIMIT {self.LIMIT_QUERY}) AS temp;'
        qs.set_optional_count_query(full_query)
        return qs 
Example #23
Source File: mixins.py    From lexpredict-contraxsuite with GNU Affero General Public License v3.0 5 votes vote down vote up
def wrap(qs: QuerySet):
        qs.__class__ = CustomCountQuerySet
        return qs 
Example #24
Source File: managers.py    From lexpredict-contraxsuite with GNU Affero General Public License v3.0 5 votes vote down vote up
def active_tasks_exist(self, task_name: str, execution_delay: datetime.datetime,
                           activity_filter: QuerySet = None) -> bool:
        has_activity = None
        work_dates = self.filter(name=task_name, status__in=UNREADY_STATES).values_list('date_work_start', flat=True)
        for date_work_start in work_dates:
            if date_work_start is None or date_work_start > execution_delay:
                return True
            if activity_filter and has_activity is None:
                has_activity = activity_filter.exists()
                if has_activity:
                    return True
        return False 
Example #25
Source File: querysets.py    From lexpredict-contraxsuite with GNU Affero General Public License v3.0 5 votes vote down vote up
def stringify_queryset(qs: QuerySet) -> str:
    sql, params = qs.query.sql_with_params()
    with connection.cursor() as cursor:
        cursor.execute('EXPLAIN ' + sql, params)
        raw_sql =  cursor.db.ops.last_executed_query(cursor, sql, params)
    raw_sql = raw_sql[len('EXPLAIN '):]
    return raw_sql 
Example #26
Source File: querysets.py    From lexpredict-contraxsuite with GNU Affero General Public License v3.0 5 votes vote down vote up
def wrap(qs: QuerySet):
        qs.__class__ = QuerySetWoCache
        return qs 
Example #27
Source File: querysets.py    From lexpredict-contraxsuite with GNU Affero General Public License v3.0 5 votes vote down vote up
def wrap(qs: QuerySet):
        qs.__class__ = CustomCountQuerySet
        return qs 
Example #28
Source File: managers.py    From lexpredict-contraxsuite with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_active_user_tasks(self) -> QuerySet:
        execution_delay = now() - datetime.timedelta(seconds=settings.USER_TASK_EXECUTION_DELAY)
        start_date_limit = now() - datetime.timedelta(seconds=3 * 24 * 60 * 60)
        return self \
            .filter(Q(main_task__isnull=True) | Q(main_task_id=F('id'))) \
            .filter(status__in=UNREADY_STATES) \
            .exclude(name__in=settings.EXCLUDE_FROM_TRACKING) \
            .filter(Q(date_start__isnull=True) | Q(date_start__gt=start_date_limit)) \
            .filter(Q(date_work_start__isnull=True) | Q(date_work_start__gt=execution_delay)) 
Example #29
Source File: querysets.py    From lexpredict-contraxsuite with GNU Affero General Public License v3.0 5 votes vote down vote up
def _clone(self):
        """
        Return a copy of the current QuerySet. A lightweight alternative
        to deepcopy().
        """
        c = self.__class__(model=self.model, query=self.query.chain(), using=self._db, hints=self._hints)
        c._sticky_filter = self._sticky_filter
        c._for_write = self._for_write
        c._prefetch_related_lookups = self._prefetch_related_lookups[:]
        c._known_related_objects = self._known_related_objects
        c._iterable_class = self._iterable_class
        c._fields = self._fields
        if hasattr(self, 'optional_count_query'):
            c.optional_count_query = self.optional_count_query
        return c 
Example #30
Source File: managers.py    From lexpredict-contraxsuite with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_queryset(self):
        return QuerySet(self.model, using=self._db)