Python django.db.models.query_utils.Q Examples
The following are 30
code examples of django.db.models.query_utils.Q().
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.query_utils
, or try the search function
.
Example #1
Source File: subqueries.py From bioforum with MIT License | 6 votes |
def delete_batch(self, pk_list, using): """ Set up and execute delete queries for all the objects in pk_list. More than one physical query may be executed if there are a lot of values in pk_list. """ # number of objects deleted num_deleted = 0 field = self.get_meta().pk for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): self.where = self.where_class() self.add_q(Q( **{field.attname + '__in': pk_list[offset:offset + GET_ITERATOR_CHUNK_SIZE]})) num_deleted += self.do_query(self.get_meta().db_table, self.where, using=using) return num_deleted
Example #2
Source File: tests.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
def test_integer_restriction_partial(self): with connection.schema_editor() as editor: index = Index( name='recent_article_idx', fields=['id'], condition=Q(pk__gt=1), ) self.assertIn( 'WHERE %s.%s' % (editor.quote_name(Article._meta.db_table), editor.quote_name('id')), str(index.create_sql(Article, schema_editor=editor)) ) editor.add_index(index=index, model=Article) self.assertIn(index.name, connection.introspection.get_constraints( cursor=connection.cursor(), table_name=Article._meta.db_table, )) editor.remove_index(index=index, model=Article)
Example #3
Source File: query.py From course-discovery with GNU Affero General Public License v3.0 | 6 votes |
def enrollable(self): """ Returns course runs that are currently open for enrollment. A course run is considered open for enrollment if its enrollment start date has passed, is now or is None, AND its enrollment end date is in the future or is None. Returns: QuerySet """ now = datetime.datetime.now(pytz.UTC) return self.filter( ( Q(enrollment_end__gt=now) | Q(enrollment_end__isnull=True) ) & ( Q(enrollment_start__lte=now) | Q(enrollment_start__isnull=True) ) )
Example #4
Source File: query.py From course-discovery with GNU Affero General Public License v3.0 | 6 votes |
def active(self): """ Returns CourseRuns that have not yet ended and meet the following enrollment criteria: - Open for enrollment - OR will be open for enrollment in the future - OR have no specified enrollment close date (e.g. self-paced courses) Returns: QuerySet """ now = datetime.datetime.now(pytz.UTC) return self.filter( ( Q(end__gt=now) | Q(end__isnull=True) ) & ( Q(enrollment_end__gt=now) | Q(enrollment_end__isnull=True) ) )
Example #5
Source File: subqueries.py From python with Apache License 2.0 | 6 votes |
def delete_batch(self, pk_list, using): """ Set up and execute delete queries for all the objects in pk_list. More than one physical query may be executed if there are a lot of values in pk_list. """ # number of objects deleted num_deleted = 0 field = self.get_meta().pk for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): self.where = self.where_class() self.add_q(Q( **{field.attname + '__in': pk_list[offset:offset + GET_ITERATOR_CHUNK_SIZE]})) num_deleted += self.do_query(self.get_meta().db_table, self.where, using=using) return num_deleted
Example #6
Source File: tests.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
def test_is_null_condition(self): with connection.schema_editor() as editor: index = Index( name='recent_article_idx', fields=['pub_date'], condition=Q(pub_date__isnull=False), ) self.assertIn( 'WHERE %s.%s IS NOT NULL' % (editor.quote_name(Article._meta.db_table), editor.quote_name("pub_date")), str(index.create_sql(Article, schema_editor=editor)) ) editor.add_index(index=index, model=Article) self.assertIn(index.name, connection.introspection.get_constraints( cursor=connection.cursor(), table_name=Article._meta.db_table, )) editor.remove_index(index=index, model=Article)
Example #7
Source File: collector.py From albatross with GNU Affero General Public License v3.0 | 6 votes |
def _get_archives_to_start(self, now): """ If the archiver is killed unexpectedly, we need to account for the special case of its "first pass", where we need to re-start should-be ongoing archivals. We also call ._handle_restarts() here to capture the special case where the stream was killed for whatever reason. """ r = Archive.objects\ .filter(started__lte=now)\ .exclude(pk__in=[a.pk for a in self.tracking]) if self.first_pass_completed: r = r.exclude(is_running=False) self.first_pass_completed = True return self._handle_restarts( r.filter(Q(stopped__gt=now) | Q(stopped__isnull=True)) ).exclude( user__status=User.STATUS_DISABLED )
Example #8
Source File: subqueries.py From openhgsenti with Apache License 2.0 | 6 votes |
def delete_batch(self, pk_list, using, field=None): """ Set up and execute delete queries for all the objects in pk_list. More than one physical query may be executed if there are a lot of values in pk_list. """ # number of objects deleted num_deleted = 0 if not field: field = self.get_meta().pk for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): self.where = self.where_class() self.add_q(Q( **{field.attname + '__in': pk_list[offset:offset + GET_ITERATOR_CHUNK_SIZE]})) num_deleted += self.do_query(self.get_meta().db_table, self.where, using=using) return num_deleted
Example #9
Source File: subqueries.py From Hands-On-Application-Development-with-PyCharm with MIT License | 6 votes |
def delete_batch(self, pk_list, using): """ Set up and execute delete queries for all the objects in pk_list. More than one physical query may be executed if there are a lot of values in pk_list. """ # number of objects deleted num_deleted = 0 field = self.get_meta().pk for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): self.where = self.where_class() self.add_q(Q( **{field.attname + '__in': pk_list[offset:offset + GET_ITERATOR_CHUNK_SIZE]})) num_deleted += self.do_query(self.get_meta().db_table, self.where, using=using) return num_deleted
Example #10
Source File: descriptors.py From cleanerversion with Apache License 2.0 | 6 votes |
def get_current_m2m_diff(self, instance, new_objects): """ :param instance: Versionable object :param new_objects: objects which are about to be associated with instance :return: (being_removed id list, being_added id list) :rtype : tuple """ new_ids = self.pks_from_objects(new_objects) relation_manager = self.__get__(instance) filter = Q(**{relation_manager.source_field.attname: instance.pk}) qs = self.through.objects.current.filter(filter) try: # Django 1.7 target_name = relation_manager.target_field.attname except AttributeError: # Django 1.6 target_name = relation_manager.through._meta.get_field_by_name( relation_manager.target_field_name)[0].attname current_ids = set(qs.values_list(target_name, flat=True)) being_removed = current_ids - new_ids being_added = new_ids - current_ids return list(being_removed), list(being_added)
Example #11
Source File: query.py From bioforum with MIT License | 6 votes |
def complex_filter(self, filter_obj): """ Return a new QuerySet instance with filter_obj added to the filters. filter_obj can be a Q object or a dictionary of keyword lookup arguments. This exists to support framework features such as 'limit_choices_to', and usually it will be more natural to use other methods. """ if isinstance(filter_obj, Q): clone = self._chain() clone.query.add_q(filter_obj) return clone else: return self._filter_or_exclude(None, **filter_obj)
Example #12
Source File: tests.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
def test_partial_index(self): with connection.schema_editor() as editor: index = Index( name='recent_article_idx', fields=['pub_date'], condition=Q( pub_date__gt=datetime.datetime( year=2015, month=1, day=1, # PostgreSQL would otherwise complain about the lookup # being converted to a mutable function (by removing # the timezone in the cast) which is forbidden. tzinfo=timezone.get_current_timezone(), ), ) ) self.assertIn( 'WHERE %s.%s' % (editor.quote_name(Article._meta.db_table), editor.quote_name("pub_date")), str(index.create_sql(Article, schema_editor=editor)) ) editor.add_index(index=index, model=Article) self.assertIn(index.name, connection.introspection.get_constraints( cursor=connection.cursor(), table_name=Article._meta.db_table, )) editor.remove_index(index=index, model=Article)
Example #13
Source File: test_indexes.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
def test_partial_gin_index_with_tablespace(self): with register_lookup(CharField, Length): index_name = 'char_field_gin_partial_idx' index = GinIndex( fields=['field'], name=index_name, condition=Q(field__length=40), db_tablespace='pg_default', ) with connection.schema_editor() as editor: editor.add_index(CharFieldModel, index) self.assertIn('TABLESPACE "pg_default" ', str(index.create_sql(CharFieldModel, editor))) constraints = self.get_constraints(CharFieldModel._meta.db_table) self.assertEqual(constraints[index_name]['type'], 'gin') with connection.schema_editor() as editor: editor.remove_index(CharFieldModel, index) self.assertNotIn(index_name, self.get_constraints(CharFieldModel._meta.db_table))
Example #14
Source File: categories.py From cito_engine with Apache License 2.0 | 6 votes |
def edit_category(request, category_id): if request.user.perms.access_level > 2: return render(request, 'unauthorized.html') page_title = 'Editing category' box_title = page_title category = get_object_or_404(Category, pk=category_id) if request.method == 'POST': form = categories.CategoryForm(request.POST) if form.is_valid(): category_type = form.cleaned_data['categoryType'] if Category.objects.filter(~Q(pk=category_id), categoryType__iexact=category_type).count() > 0: errors = ['Category with name \"%s\" already exists.' % category_type] else: query = dict(categoryType=category_type) Category.objects.filter(pk=category_id).update(**query) success_msg = 'Category Updated.' return redirect('/categories/') else: form = categories.CategoryForm(instance=category) return render(request, 'generic_form.html', locals())
Example #15
Source File: aggregates.py From bioforum with MIT License | 6 votes |
def as_sql(self, compiler, connection, **extra_context): if self.filter: if connection.features.supports_aggregate_filter_clause: filter_sql, filter_params = self.filter.as_sql(compiler, connection) template = self.filter_template % extra_context.get('template', self.template) sql, params = super().as_sql(compiler, connection, template=template, filter=filter_sql) return sql, params + filter_params else: copy = self.copy() copy.filter = None condition = When(Q()) source_expressions = copy.get_source_expressions() condition.set_source_expressions([self.filter, source_expressions[0]]) copy.set_source_expressions([Case(condition)] + source_expressions[1:]) return super(Aggregate, copy).as_sql(compiler, connection, **extra_context) return super().as_sql(compiler, connection, **extra_context)
Example #16
Source File: pluginservers.py From cito_engine with Apache License 2.0 | 6 votes |
def view_all_pluginservers(request): render_vars = dict() if request.user.perms.access_level > 3: return render(request, 'unauthorized.html') search_form = pluginserver.PluginSearchForm() if request.method == "POST": search_form = pluginserver.PluginSearchForm(request.POST) if search_form.is_valid(): search_term = search_form.cleaned_data.get('search_term') render_vars['plugins'] = Plugin.objects.filter(Q(name__icontains=search_term) | Q(description__icontains=search_term)) render_vars['search_term'] = search_term if request.method == "GET": try: render_vars['servers'] = PluginServer.objects.all() except PluginServer.DoesNotExist: render_vars['servers'] = None render_vars[search_form] = search_form return render(request, 'view_all_pluginservers.html', render_vars)
Example #17
Source File: test_update_fields.py From rotest with MIT License | 6 votes |
def test_update_specific_resource_fields(self): """Assert that updating specific resource - works.""" response, _ = self.requester(json_data={ "resource_descriptor": { "type": "rotest.management.models.ut_models." "DemoResourceData", "properties": { "name": "available_resource1" } }, "changes": { "reserved": "A_User" } }) self.assertEqual(response.status_code, http_client.NO_CONTENT) resources = DemoResourceData.objects.filter(name="available_resource1") for resource in resources: self.assertEqual(resource.reserved, "A_User") resources = DemoResourceData.objects.filter( ~Q(name="available_resource1")) for resource in resources: self.assertNotEqual(resource.reserved, "A_User")
Example #18
Source File: pluginservers.py From cito_engine with Apache License 2.0 | 6 votes |
def edit_pluginserver(request, pluginserver_id): if request.user.perms.access_level > 2: return render(request, 'unauthorized.html') page_title = 'Editing plugin server' box_title = page_title ps = get_object_or_404(PluginServer, pk=pluginserver_id) if request.method == "POST": form = pluginserver.PluginServerForm(request.POST) if form.is_valid(): name = form.cleaned_data.get('name') url = form.cleaned_data.get('url') status = form.cleaned_data.get('status') ssl_verify = form.cleaned_data.get('ssl_verify') if PluginServer.objects.filter(~Q(pk=pluginserver_id), url__iexact=url).count() > 0: errors = ['A server with URL: %s already exists' % url] else: query = dict(name=name, url=url, status=status, ssl_verify=ssl_verify) PluginServer.objects.filter(pk=pluginserver_id).update(**query) return redirect('/pluginservers/view/%s/' % pluginserver_id) else: form = pluginserver.PluginServerForm(instance=ps) return render(request, 'generic_form.html', locals())
Example #19
Source File: teams.py From cito_engine with Apache License 2.0 | 6 votes |
def edit_team(request, team_id): if request.user.perms.access_level > 2: return render(request, 'unauthorized.html') page_title = 'Editing team' box_title = page_title team = get_object_or_404(Team, pk=team_id) if request.method == 'POST': form = teams.TeamForm(request.POST) if form.is_valid(): team_name = form.cleaned_data.get('name') if Team.objects.filter(~Q(pk=team_id), name__iexact=team_name).count() > 0: errors = ['Team with name \"%s\" already exists.' % team_name] else: team.name = team_name team.description = form.cleaned_data.get('description') team.members = form.cleaned_data.get('members') team.save() return redirect('/teams/') else: form = teams.TeamForm(instance=team) return render(request, 'generic_form.html', locals())
Example #20
Source File: test_resource_manager.py From rotest with MIT License | 6 votes |
def test_lock_already_locked_resource(self): """Lock an already locked resource & validate failure. * Validates the DB initial state. * Locks an already locked resource, using resource client. * Validates a ResourceUnavailableError is raised. """ resources_num = DemoResourceData.objects.filter(~Q(owner=""), name=self.LOCKED1_NAME).count() self.assertEqual(resources_num, 1, "Expected 1 locked " "resource with name %r in DB found %d" % (self.LOCKED1_NAME, resources_num)) descriptor = Descriptor(DemoResource, name=self.LOCKED1_NAME) self.assertRaises(ResourceUnavailableError, self.client._lock_resources, descriptors=[descriptor], timeout=self.LOCK_TIMEOUT)
Example #21
Source File: add_project_tag_2_3_tests.py From seqr with GNU Affero General Public License v3.0 | 6 votes |
def test_normal_command(self): out = StringIO() call_command('add_project_tag', '--project={}'.format(TAG_ARGUMENTS["project"]), '--name={}'.format(TAG_ARGUMENTS["name"]), '--order={}'.format(TAG_ARGUMENTS["order"]), '--category={}'.format(TAG_ARGUMENTS["category"]), '--description={}'.format(TAG_ARGUMENTS["description"]), '--color={}'.format(TAG_ARGUMENTS["color"]), stdout = out) self.assertEqual('', out.getvalue()) project = Project.objects.get(Q(name = TAG_ARGUMENTS["project"])) variantTagType = VariantTagType.objects.get(name=TAG_ARGUMENTS["name"], project=project) tag_info = { "project": variantTagType.project.name, "name": variantTagType.name, "order": variantTagType.order, "category": variantTagType.category, "description": variantTagType.description, "color": variantTagType.color } self.assertDictEqual(tag_info, TAG_ARGUMENTS)
Example #22
Source File: subqueries.py From python2017 with MIT License | 6 votes |
def delete_batch(self, pk_list, using): """ Set up and execute delete queries for all the objects in pk_list. More than one physical query may be executed if there are a lot of values in pk_list. """ # number of objects deleted num_deleted = 0 field = self.get_meta().pk for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): self.where = self.where_class() self.add_q(Q( **{field.attname + '__in': pk_list[offset:offset + GET_ITERATOR_CHUNK_SIZE]})) num_deleted += self.do_query(self.get_meta().db_table, self.where, using=using) return num_deleted
Example #23
Source File: copy_project_tags.py From seqr with GNU Affero General Public License v3.0 | 6 votes |
def handle(self, *args, **options): source_project_name = options['source'] target_project_name = options['target'] source_project = Project.objects.get(Q(name=source_project_name) | Q(guid=source_project_name)) target_project = Project.objects.get(Q(name=target_project_name) | Q(guid=target_project_name)) tags = VariantTagType.objects.filter(project=source_project) for tag in tags: tag.pk = None tag.id = None tag.project = target_project tag.save() logger.info('Saved tag %s (new id = %d)' % (tag.name, tag.id))
Example #24
Source File: fields.py From cleanerversion with Apache License 2.0 | 5 votes |
def get_reverse_related_filter(self, obj): base_filter = dict() timestamp_q = None for lh_field, rh_field in self.related_fields: if isinstance(obj, Versionable) and \ rh_field.attname == \ Versionable.VERSION_IDENTIFIER_FIELD: base_filter.update(**{ Versionable.OBJECT_IDENTIFIER_FIELD: getattr(obj, lh_field.attname)}) if hasattr(obj, 'as_of') and obj.as_of is not None: start_date_q = Q(version_start_date__lt=obj.as_of) end_date_q = Q(version_end_date__gte=obj.as_of) | Q( version_end_date__isnull=True) timestamp_q = start_date_q & end_date_q else: base_filter.update( **{rh_field.attname: getattr(obj, lh_field.attname)}) base_q = Q(**base_filter) if timestamp_q: base_q &= timestamp_q descriptor_filter = self.get_extra_descriptor_filter(obj) if isinstance(descriptor_filter, dict): return base_q & Q(**descriptor_filter) elif descriptor_filter: return base_q & descriptor_filter return base_q
Example #25
Source File: tests.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_ops_class_partial(self): index = Index( name='test_ops_class_partial', fields=['body'], opclasses=['text_pattern_ops'], condition=Q(headline__contains='China'), ) with connection.schema_editor() as editor: editor.add_index(IndexedArticle2, index) with editor.connection.cursor() as cursor: cursor.execute(self.get_opclass_query % 'test_ops_class_partial') self.assertCountEqual(cursor.fetchall(), [('text_pattern_ops', 'test_ops_class_partial')])
Example #26
Source File: test_resource_manager.py From rotest with MIT License | 5 votes |
def test_lock_unavailable_resource_timeout(self): """Lock an already locked resource & validate failure after timeout. * Validates the DB initial state. * Locks an already locked resource, using resource client. * Validates a ResourceUnavailableError is raised. * Validates 'lock_resources' duration is greater then the timeout. """ resources_num = DemoResourceData.objects.filter(~Q(owner=""), name=self.LOCKED1_NAME).count() self.assertEqual(resources_num, 1, "Expected 1 locked " "resource with name %r in DB found %d" % (self.LOCKED1_NAME, resources_num)) descriptor = Descriptor(DemoResource, name=self.LOCKED1_NAME) start_time = time.time() self.assertRaises(ResourceUnavailableError, self.client._lock_resources, descriptors=[descriptor], timeout=self.LOCK_TIMEOUT) duration = time.time() - start_time self.assertGreaterEqual(duration, self.LOCK_TIMEOUT, "Waiting for " "resources took %.2f seconds, but should take " "at least %d" % (duration, self.LOCK_TIMEOUT))
Example #27
Source File: tests.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_condition_ignored(self): index = Index( name='test_condition_ignored', fields=['published'], condition=Q(published=True), ) with connection.schema_editor() as editor: # This would error if condition weren't ignored. editor.add_index(Article, index) self.assertNotIn( 'WHERE %s.%s' % (editor.quote_name(Article._meta.db_table), 'published'), str(index.create_sql(Article, editor)) )
Example #28
Source File: subqueries.py From python2017 with MIT License | 5 votes |
def update_batch(self, pk_list, values, using): self.add_update_values(values) for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): self.where = self.where_class() self.add_q(Q(pk__in=pk_list[offset: offset + GET_ITERATOR_CHUNK_SIZE])) self.get_compiler(using).execute_sql(NO_RESULTS)
Example #29
Source File: query.py From python2017 with MIT License | 5 votes |
def _filter_or_exclude(self, negate, *args, **kwargs): if args or kwargs: assert self.query.can_filter(), \ "Cannot filter a query once a slice has been taken." clone = self._clone() if negate: clone.query.add_q(~Q(*args, **kwargs)) else: clone.query.add_q(Q(*args, **kwargs)) return clone
Example #30
Source File: test_resource_manager.py From rotest with MIT License | 5 votes |
def test_lock_multiple_matches(self): """Lock a resource, parameters matching more then one result. * Validates the DB initial state. * Locks a resource using parameters that match more than one resource, using resource client. * Validates only one resource returned. * Validates there is still 2 available resource with same parameters. """ common_parameters = {'ip_address': "1.1.1.1"} resources_num = DemoResourceData.objects.filter(owner="", **common_parameters).count() self.assertEqual(resources_num, 2, "Expected 2 available " "resources with parameters %r in DB found %d" % (common_parameters, resources_num)) descriptor = Descriptor(DemoResource, **common_parameters) resources = self.client._lock_resources(descriptors=[descriptor], timeout=self.LOCK_TIMEOUT) resources_num = len(resources) self.assertEqual(resources_num, 1, "Expected list with 1 " "resource in it but found %d" % resources_num) locked_resource_name = resources[0].name resources_num = descriptor.type.DATA_CLASS.objects.filter(~Q(owner=""), **common_parameters).count() self.assertEqual(resources_num, 2, "Expected 2 unlocked " "resource with name %r in DB, found %d" % (locked_resource_name, resources_num))